-
Notifications
You must be signed in to change notification settings - Fork 1
/
promise-rejection-generator.js
48 lines (36 loc) · 1.15 KB
/
promise-rejection-generator.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
/*
The goal is to demonstrate how to handle async queues in a proper maner, withing error handling.
To achieve this goal we use a simple queue of async tasks.
This can be solved using Promise.all and a generator to generates the queue.
*/
const axios = require('axios')
const assert = require('assert')
const results = []
const urls = [
'http://localhost:3000/endpointThatWorks',
'http://localhost:3000/nowhere'
]
function* generatePromise (urls) {
let index = 0;
while (urls) {
yield { done: false, value: axios(urls[index]).catch(null) }
index++;
}
}
async function mountPromiseQueue (promise) {
let result = await promise.catch(null);
return result;
};
(async (urls) => {
let promiseQueue = [];
let promisesListResult = [];
for await (const promise of generatePromise(url)) {
prmomiseQueue.push(promise);
}
for await (const promise of promiseQueue) {
let result = promise.value;
promisesListResult.push(result);
}
const haveSomePromiseFailed = promisesListResult.filter((value) => value == null).length > 0
assert(haveSomePromiseFailed, true)
})(urls)