-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
158 lines (138 loc) · 3.16 KB
/
examples.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Future from './future.js';
const { log, clear } = console;
const response = new Future((resolve) => {
setTimeout(() => {
resolve(200);
}, 1500);
});
log(response, '\n', response.value);
// value is auto-unwrapped the first time a Future resolves
log(await response);
response.then((value) => {
log(
`this resolves with ${value} on the next event loop\n after previous await, right before next await`
);
});
// don't have to await again
log(response.value);
log(response.value);
log(response.value);
debugger;
clear();
const problematicResponse = new Future((_resolve, reject) => {
throw new Error(`Your HDD exploded`);
setTimeout(() => {
reject(404);
}, 1500);
});
log(problematicResponse, '\n', problematicResponse.value);
try {
log(await problematicResponse);
} catch (e) {
log(problematicResponse.value === e ? e : 'different');
}
debugger;
clear();
const asResFunc = async (_, catColor, catAge) => {
return await new Promise((resolve) => {
setTimeout(() => {
resolve({ status: 200, catColor, catAge });
}, 1500);
});
};
const asyncResponse = new Future(asResFunc, 0, 'green-ish blue', Infinity);
log(asyncResponse, '\n', asyncResponse.value);
log(
await asyncResponse.catch((e) => {
log(e);
})
);
log(asyncResponse.value);
debugger;
clear();
const problematicAsyncResponse = new Future(async () => {
throw new Error(`I/O problems, check your wifi adapter`);
});
log(problematicAsyncResponse, '\n', problematicAsyncResponse.value);
try {
log(await problematicAsyncResponse);
} catch (_e) {
log(problematicAsyncResponse.value);
}
debugger;
clear();
async function fetchData(signal) {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {
signal
});
return await response.json();
}
const cancelledFetch = new Future(fetchData);
try {
// very contrived example just to be short
cancelledFetch.abort();
log(await cancelledFetch);
} catch (e) {
log(e);
}
const control = new AbortController();
const cancelledFetch2 = new Future(fetchData, { signal: control.signal });
try {
// cancelledFetch2.abort(); // not a function
control.abort();
log(await cancelledFetch2);
} catch (e) {
log(e);
}
debugger;
clear();
const cancelledFuture = new Future((res, rej, signal) => {
signal.addEventListener(
'abort',
() => {
rej(signal.reason);
},
{ once: true }
);
// never resolves
setTimeout(() => {
res(200);
}, 1500);
});
try {
cancelledFuture.abort(`I don't want this`);
log(await cancelledFuture);
} catch (e) {
log(e);
}
debugger;
clear();
function fileAsync(path) {
return new Promise((res) => {
setTimeout(() => {
res(path);
}, 1000);
});
}
function numberAsync(num) {
return new Promise((res) => {
setTimeout(() => {
res(num);
}, 1000);
});
}
async function longTask() {
const file1 = fileAsync('bunny.png');
const file2 = fileAsync('carrot.png');
let number = numberAsync(1);
// avoid awaiting in a loop
number = await number;
// long action
for (let i = 0; i < 100000; i++) {
number = i * 3;
}
// actual use of data
return (await file1) + (await file2) + number;
}
log(await longTask());
debugger;