-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (33 loc) · 921 Bytes
/
index.js
File metadata and controls
49 lines (33 loc) · 921 Bytes
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
'use strict';
// queueAsync :: [ x -> Promise ] -> Promise
/* Takes an Array of functions that return promises, given particular
* input, and performs them in series.
*/
function queueAsync (promiseFuncs) {
const result = promiseFuncs.reduce(async (prev, next) => {
try {
const result = await prev;
return next(result);
} catch (err) {
console.error(err);
throw err;
}
}, true);
}
function test () {
const queue = [
(dat) => new Promise((res) => setTimeout(() => res(dat), 1000)),
console.log,
(dat) => new Promise((res) => setTimeout(() => res('Oi!'), 1000)),
console.log,
(dat) => new Promise((r, x) => setTimeout(() => x('Fail!'), 1000)),
];
(async () => {
// Should log in intervals of 1 second:
// 1. "true"
// 2. "Oi!"
// Should then throw an error: "Fail!"
await queueAsync(queue);
})();
}
test();