This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
tracking_the_scalpel.js
45 lines (33 loc) · 1.62 KB
/
tracking_the_scalpel.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
/*
1 - Tracking The Scalpel (20/1/2020)
The village crows own an old scalpel that they occasionally use on special missions—say, to cut through screen doors or packaging. To be able to quickly track it down, every time the scalpel is moved to another nest, an entry is added to the storage of both the nest that had it and the nest that took it, under the name "scalpel", with its new location as the value.
This means that finding the scalpel is a matter of following the breadcrumb trail of storage entries, until you find a nest where that points at the nest itself.
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?
*/
"use strict";
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 Shop
locateScalpel2(bigOak).then(console.log);
// → Butcher Shop