Skip to main content

Delay the Resolution of Each Promise - Solution & Explanation

MediumPremiumFree on FleetCode4 min read
Practice this problem

Problem Statement

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 promises
  • 10 <= ms <= 500
  • 1 <= functions.length <= 10

Approach Overview

Problem 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.

Solution

Code

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Promise.then with setTimeoutO(n)O(n)Standard solution. Best when demonstrating direct promise chaining and async behavior.
Async/Await WrapperO(n)O(n)Preferred for readability when writing sequential async logic.

Video Solution

Javascript Promises vs Async Await EXPLAINED (in 5 minutes) • Roberts Dev Talk • 650,152 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Delay the Resolution of Each Promise easy or hard?
The problem is generally rated Medium because it requires understanding asynchronous execution and promise resolution order. The coding part is short, but recognizing that the delay must happen after the promise resolves is the key insight.
Delay the Resolution of Each Promise Python/Java solution
The exact problem targets JavaScript because it relies on Promise behavior. In Python or Java, similar logic can be implemented using asynchronous constructs like asyncio futures in Python or CompletableFuture with delayed executors in Java.
How to solve Delay the Resolution of Each Promise in O(n)?
Map over the input array and convert each promise using promise.then. Inside the then callback, return a new Promise that calls setTimeout for t milliseconds before resolving with the original value. This ensures the delay begins only after the original promise finishes resolving.
What is the best approach for Delay the Resolution of Each Promise?
The most common solution wraps each promise using promise.then and delays the returned value with setTimeout. After the original promise resolves, a new promise waits t milliseconds before resolving with the same value. This processes each promise once, giving O(n) time and O(n) space complexity.
Is Delay the Resolution of Each Promise asked at Google/Amazon/Meta?
Promise and asynchronous control flow questions frequently appear in JavaScript-focused interviews at companies like Amazon and Meta. While this exact problem may vary, similar tasks that test promise chaining, async/await behavior, and event loop understanding are common.
What data structure is used in Delay the Resolution of Each Promise?
The problem mainly relies on arrays and JavaScript Promise objects. The array stores the list of promises, and each promise is transformed using promise chaining or async/await logic combined with setTimeout for delayed resolution.
What is the time complexity of Delay the Resolution of Each Promise?
The time complexity is O(n) because the algorithm iterates through the array once and wraps each promise with a delayed resolver. Each wrapper adds a constant-time asynchronous operation. Space complexity is also O(n) since a new promise is created for every input promise.

Ready to solve this problem?

Practice Delay the Resolution of Each Promise with our built-in code editor and test cases.

Practice on FleetCode