Problem

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.

Examples

Example 1:

1
2
3
4
5
6
7
Input: 
functions = [
() => new Promise((resolve) => setTimeout(resolve, 30))
], 
ms = 50
Output: [80]
Explanation: The promise from the array would have resolved after 30 ms, but it was delayed by 50 ms, thus 30 ms + 50 ms = 80 ms.

Example 2:

1
2
3
4
5
6
7
8
Input: 
functions = [
() => new Promise((resolve) => setTimeout(resolve, 50)),
() => new Promise((resolve) => setTimeout(resolve, 80))
], 
ms = 70
Output: [120,150]
Explanation: The promises from the array would have resolved after 50 ms and 80 ms, but they were delayed by 70 ms, thus 50 ms + 70 ms = 120 ms and 80 ms + 70 ms = 150 ms.

Example 3:

1
2
3
4
5
6
7
Input: 
functions = [
() => new Promise((resolve, reject) => setTimeout(reject, 20)), 
() => new Promise((resolve, reject) => setTimeout(reject, 100))
], 
ms = 30
Output:[50,130]

Constraints:

  • functions is an array of functions that return promises
  • 10 <= ms <= 500
  • 1 <= functions.length <= 10

Solution

Method 1 – Promise Wrapping with Delay

Intuition

We want to delay the resolution or rejection of each promise returned by the input functions by ms milliseconds. This can be achieved by wrapping each function so that after the original promise settles, we wait for ms ms before resolving or rejecting with the same value.

Approach

  1. For each function in the input array, create a new function.
  2. The new function calls the original function to get its promise.
  3. When the original promise resolves or rejects, set a timeout for ms ms, then resolve or reject with the same value.
  4. Return the array of wrapped functions.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    delayAll(functions, ms) {
        return functions.map(fn => () => new Promise((resolve, reject) => {
            fn().then(
                v => setTimeout(() => resolve(v), ms),
                e => setTimeout(() => reject(e), ms)
            );
        }));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    delayAll(functions: (() => Promise<any>)[], ms: number): (() => Promise<any>)[] {
        return functions.map(fn => () => new Promise((resolve, reject) => {
            fn().then(
                v => setTimeout(() => resolve(v), ms),
                e => setTimeout(() => reject(e), ms)
            );
        }));
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the number of functions, since we wrap each function once.
  • 🧺 Space complexity: O(n), for the array of wrapped functions.