Delay the Resolution of Each Promise - Problem

Delay the Resolution of Each Promise

Imagine you're building a system that needs to add artificial delays to promise-returning functions for rate limiting or throttling purposes. You have an array of functions that return promises, and you want to create a new array where each function introduces an additional delay before resolving or rejecting.

Your Task: Given an array functions and a delay duration ms, return a new array of functions. Each function in the new array should:

  • Execute the corresponding original function
  • Wait for the original promise to resolve or reject
  • Add an additional delay of ms milliseconds
  • Then resolve or reject with the same value/error

Think of it like adding a "cooldown period" to each promise, similar to how video games add cooldowns to abilities!

Input: An array of functions that return promises, and a delay duration in milliseconds

Output: A new array of functions that return delayed promises

Input & Output

example_1.js โ€” JavaScript Basic Delay
$ Input: functions = [() => Promise.resolve(1), () => Promise.resolve(2)], ms = 100
โ€บ Output: Array of 2 functions that resolve to [1, 2] after 100ms additional delay each
๐Ÿ’ก Note: Each function in the returned array will execute the original function, wait for its result, then add 100ms delay before resolving with the same value
example_2.js โ€” JavaScript With Rejection
$ Input: functions = [() => Promise.resolve("success"), () => Promise.reject("error")], ms = 200
โ€บ Output: First function resolves to "success" after 200ms delay, second rejects with "error" after 200ms delay
๐Ÿ’ก Note: Both successful and failed promises get the same delay treatment - the delay is added regardless of the outcome
example_3.js โ€” JavaScript Empty Array
$ Input: functions = [], ms = 500
โ€บ Output: []
๐Ÿ’ก Note: Edge case: when given an empty array of functions, return an empty array of delayed functions

Constraints

  • 1 โ‰ค functions.length โ‰ค 1000
  • 0 โ‰ค ms โ‰ค 104
  • Each function in the input array returns a Promise
  • The delay should be added after the original promise settles
  • Preserve the original promise's resolution/rejection value and reason

Visualization

Tap to expand
OriginalPromiseExecutePromiseSettlesโœ“ or โœ—Add DelayTimerโฑ msReturnResultSame ValueTimeline: Original Duration + Additional DelayOriginal Promise TimeAdded Delay (ms)Key Insight: Delay happens AFTER original promise settlesBoth successful and failed promises get the same delay treatment1234
Understanding the Visualization
1
Original Promise Executes
The original function runs and returns a promise
2
Wait for Settlement
Wait for the promise to either resolve or reject
3
Add Artificial Delay
After settlement, start the additional delay timer
4
Return Same Outcome
After delay completes, resolve/reject with original value
Key Takeaway
๐ŸŽฏ Key Insight: Use promise chaining to add delays after the original promise settles, preserving both successful results and error states while adding consistent timing delays.
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
28.4K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen