-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_eleven.js
68 lines (58 loc) · 1.54 KB
/
chapter_eleven.js
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
// asynchronous programming
// generator function
function* iterator(n) {
for (let current = n; current < 50; current *= n) {
yield current;
}
}
for (let current of iterator(3)) {
console.log(current);
}
const it = iterator(3)[Symbol.iterator]();
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());
//tracking the scalpel
async function locateScalpel(nest) {
let current = nest.name;
while (nest) {
let next = await anyStorage(nest, current, "scalpel");
if (next == current) return current;
current = next;
}
}
//building promise.all
function Promise_all(promises) {
return new Promise((resolve, reject) => {
let returnVal = [];
let promiseCount = promises.length;
for (let i = 0; i < promises.length; i++) {
promises[i].then(value => {
returnVal[i] = value;
promiseCount -= 1;
if (promiseCount == 0) resolve(returnVal);
}).catch(e => reject(e));
}
if (promises.length == 0) resolve(returnVal);
})
}
// Test code.
Promise_all([]).then(array => {
console.log("This should be []:", array);
});
function soon(val) {
return new Promise(resolve => {
setTimeout(() => resolve(val), Math.random() * 500);
});
}
Promise_all([soon(1), soon(2), soon(3)]).then(array => {
console.log("This should be [1, 2, 3]:", array);
});
Promise_all([soon(1), Promise.reject("X"), soon(3)]).then(array => {
console.log("We should not get here");
}).catch(error => {
if (error != "X") {
console.log("Unexpected failure:", error);
}
});