-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.js
43 lines (41 loc) · 1019 Bytes
/
retry.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
const arr = [1, 2, 3, 2, 4];
const removeAllInstances = (arr = []) => {
filtered = arr.filter((val) => {
const lastIndex = arr.lastIndexOf(val);
const firstIndex = arr.indexOf(val);
return lastIndex === firstIndex;
});
return filtered;
};
console.log(removeAllInstances(arr));
let n = 1;
async function retryRequest(promiseFunc, nrOfRetries) {
// Write your code here
// let response = null;
await promiseFunc()
.then((resp) => {
response = resp;
console.log(resp);
return response;
})
.catch(async (err) => {
await retryRequest(promiseFunc, nrOfRetries - 1);
});
return response;
}
let hasFailed = false;
function getUserInfo() {
return new Promise((resolve, reject) => {
if (!hasFailed) {
hasFailed = true;
reject('Exception!');
} else {
resolve('Fetched user!');
}
});
}
let promise = retryRequest(getUserInfo, 3);
if (promise) {
promise.then((result) => console.log(result)).catch((error) => console.log('Error!'));
}
module.exports.retryRequest = retryRequest;