js0020

使用promise 的简单例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Creating a simple promise</title>
<link rel="stylesheet" href="../assert.css">
<script src="../assert.js"></script>
</head>
<body>
<script>
"use script";

const ninjaPromise = new Promise((resolve, reject) => {
resolve("Hatori");
//reject("An error resolving a promise!");
});

ninjaPromise.then(ninja => {
assert(ninja === "Hatori", "We were promised Hatori!");
}, err => {
fail("There shouldn’t be an error");
});
</script>
</body>
</html>