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.
Example 1:
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:
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:
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 promises10 <= ms <= 5001 <= functions.length <= 10Problem Overview: You receive an array of promises and a delay time t in milliseconds. Each promise should resolve with the same value as the original, but only after waiting an additional t milliseconds once the original promise resolves.
Approach 1: Promise Chaining with setTimeout (O(n) time, O(n) space)
Iterate through the input array and transform each promise using promise.then(...). Inside the then callback, wrap the resolved value in a new Promise and delay the resolution using setTimeout. The key idea is that the delay should start after the original promise resolves, not before. Each promise becomes promise.then(v => new Promise(res => setTimeout(() => res(v), t))). This approach works well because promise chaining preserves the original resolution value while inserting asynchronous delay logic.
Time complexity is O(n) since you iterate through the list once to wrap each promise. Space complexity is O(n) because a new promise object is created for every original promise. This technique relies heavily on concepts from promises and asynchronous execution.
Approach 2: Async/Await Wrapper Function (O(n) time, O(n) space)
Create a helper async function that awaits the original promise, then waits again using a timer-based promise. For example, first await promise to get the value, then await new Promise(res => setTimeout(res, t)). Finally return the resolved value. Mapping the input array to this wrapper produces the delayed promises array. The logic becomes easier to read when working with async/await, especially for developers who prefer sequential asynchronous flow over chained callbacks.
The runtime remains O(n) since each promise is processed once. Space usage is also O(n) due to the creation of new wrapped promises. The underlying mechanism still uses the JavaScript event loop and timer queue, concepts central to JavaScript asynchronous programming.
Recommended for interviews: The promise chaining approach using then and setTimeout. It directly demonstrates understanding of promise resolution flow and asynchronous control. Interviewers expect you to show that the delay happens after the promise resolves, not before. The async/await version is equally correct but often considered syntactic sugar over the same mechanism.
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Promise.then with setTimeout | O(n) | O(n) | Standard solution. Best when demonstrating direct promise chaining and async behavior. |
| Async/Await Wrapper | O(n) | O(n) | Preferred for readability when writing sequential async logic. |
Javascript Promises vs Async Await EXPLAINED (in 5 minutes) • Roberts Dev Talk • 650,152 views views
Watch 9 more video solutions →Practice Delay the Resolution of Each Promise with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor