-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2821-delay-the-resolution-of-each-promise.js
More file actions
36 lines (35 loc) · 1.24 KB
/
2821-delay-the-resolution-of-each-promise.js
File metadata and controls
36 lines (35 loc) · 1.24 KB
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
/**
* 2821. Delay the Resolution of Each Promise
* https://leetcode.com/problems/delay-the-resolution-of-each-promise/
* Difficulty: Medium
*
* Given an array functions and a number ms, return a new array of functions.
* - functions is an array of functions that return promises.
* - ms represents the delay duration in milliseconds. It determines the amount of time to wait
* before resolving or rejecting each promise in the new array.
*
* Each function in the new array should return a promise that resolves or rejects after an
* additional delay of ms milliseconds, preserving the order of the original functions array.
*
* The delayAll function should ensure that each promise from functions is executed with a
* delay, forming the new array of functions returning delayed promises.
*/
/**
* @param {Array<Function>} functions
* @param {number} ms
* @return {Array<Function>}
*/
var delayAll = function(functions, ms) {
return functions.map(fn => {
return async function() {
try {
const result = await fn();
await new Promise(resolve => setTimeout(resolve, ms));
return result;
} catch (error) {
await new Promise(resolve => setTimeout(resolve, ms));
throw error;
}
};
});
};