-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 11.js
72 lines (63 loc) · 2.55 KB
/
Chapter 11.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
69
70
71
72
/*Q-1: Write an async function locateScalpel that does this, starting at the nest on which it runs. You can use the anyStorage function defined earlier to access storage in arbitrary nests. The scalpel has been going around long enough that you may assume that every nest has a "scalpel" entry in its data storage.
Next, write the same function again without using async and await.
Do request failures properly show up as rejections of the returned promise in both versions? How? */
//Solution:
async function locateScalpel(nest) {
let current = nest.name;
for (;;) {
let next = await anyStorage(nest, current, "scalpel");
if (next == current) return current;
current = next;
}
}
function locateScalpel2(nest) {
function loop(current) {
return anyStorage(nest, current, "scalpel").then(next => {
if (next == current) return current;
else return loop(next);
});
}
return loop(nest.name);
}
locateScalpel(bigOak).then(console.log);
// → Butcher's Shop
locateScalpel2(bigOak).then(console.log);
// → Butcher's Shop
/*Q-2: Given an array of promises, Promise.all returns a promise that waits for all of the promises in the array to finish. It then succeeds, yielding an array of result values.
If a promise in the array fails, the promise returned by all fails too, with the failure reason from the failing promise.
Implement something like this yourself as a regular function called Promise_all. Remember that after a promise has succeeded or failed, it can’t succeed or fail again,
and further calls to the functions that resolve it are ignored. This can simplify the way you handle the failure of your promise.*/
//Solution:
function Promise_all(promises) {
return new Promise((resolve, reject) => {
let results = [];
let pending = promises.length;
for (let i = 0; i < promises.length; i++) {
promises[i].then(result => {
results[i] = result;
pending--;
if (pending == 0) resolve(results);
}).catch(reject);
}
if (promises.length == 0) resolve(results);
});
}
// 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);
}
});