Repository navigation
Expand file tree
/
Copy pathgenerator.html
More file actions
76 lines (58 loc) · 1.75 KB
/
Copy pathgenerator.html
File metadata and controls
76 lines (58 loc) · 1.75 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// function* foo(x) {
// yield x + 1;
// yield x + 2;
// return x + 3;
// }
// console.log(foo(1))
// function* countAppleSales() {
// var saleList = [3, 7, 5];
// for (var i = 0; i < saleList.length; i++) {
// console.log(yield saleList[i])
// }
// }
// countAppleSales(12)
// function * gen () {
// yield 1
// yield 2
// yield 3
// }
const render = log = console.log
const asyncApi = (num) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (typeof num !== 'number') {
resolve({ err: 'param error' })
}
num += 10
resolve({ data: num })
}, 100);
})
}
// asyncApi(0).then(render).catch(log) // 10
// asyncApi(5).then(render).catch(log) // 15
// asyncApi(10).then(render).catch(log) // 20
// Promise.all([asyncApi(0), asyncApi(5), asyncApi(10)]).then(render).catch(log) // [ 10, 15, 20 ]
asyncApi(0).then(render).catch(log) // 10
asyncApi(false).then(render).catch(log) // param error
asyncApi(10).then(render).catch(log) // 20
Promise.all([asyncApi(0), asyncApi(false), asyncApi(10)]).then(render).catch(log)
async function add () {
let data = await asyncApi(0)
render(data)
let data2 = await asyncApi(data.data)
render(data2)
let data3 = await asyncApi(data2.data)
render(data3)
}
</script>
</html>