-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-await.js
50 lines (43 loc) · 985 Bytes
/
async-await.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
(function () {
const Promise = require('Promise');
const wrap_gen = g =>
({
next: () => {
try {
const value = g.next();
if (value.done) {
return value;
}
return {
done: false,
value: value,
};
}
catch (e) {
return { done: true };
}
},
});
const g_return = value =>
({ done: true, value: value });
const awaiter = g => new Promise(resolve => {
let fulfilled_value;
const fulfilled = value => {
fulfilled_value = value;
step(g.next());
};
const step = result => {
result.done ?
resolve(result.value) :
new Promise(resolve => { resolve(result.value); }).then(fulfilled);
};
g = g(() => fulfilled_value);
step(g.next());
});
const async = g =>
awaiter(rv => wrap_gen(g(rv)()));
module.exports = {
async: async,
g_return: g_return,
};
})();