-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (50 loc) · 1.5 KB
/
index.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
const Promessa = require("./Promessa.js");
function sum(...args) {
let total = 0;
return new Promessa(function (resolve, reject) {
setTimeout(function () {
for (const arg of args) {
if (typeof arg !== "number") {
reject(`Invalid argument: ${arg}`);
}
total += arg;
}
resolve(total);
}, 500);
});
}
const main = Promessa.async(function* () {
try {
const a = yield sum(1);
console.log("a", a);
const b = yield sum(1, 1);
console.log("b", b);
const c = yield 3;
console.log("c", c);
const d = yield sum(1, 1)
.then((value) => sum(value, 2));
console.log("d", d);
const e = yield Promessa.async(function* () {
const e1 = yield 2;
console.log("e1", e1);
const e2 = yield sum(1, 2);
console.log("e2", e2);
return sum(e1, e2);
})();
console.log("e", e);
const f = yield Promessa.async(function* () {
return new Promessa((resolve, reject) => {
resolve(new Promessa((resolve, reject) => {
resolve(sum(6));
}));
});
})();
console.log("f", f);
const result = yield sum(a, b, c, d, e, f);
console.log("result", result);
const err = yield sum("incorrect");
} catch (error) {
console.error(error);
}
});
main();