Delay the Resolution of Each Promise - Problem

You are given a table async_functions that contains function execution records and their completion times. Each function has an execution_id, function_name, original_completion_time (in milliseconds), and a delay_ms value that represents additional delay to be added.

Task: Write a SQL query to calculate the delayed completion time for each function by adding the delay_ms to the original_completion_time. Return the results ordered by the delayed completion time to preserve the execution sequence.

The query should return:

  • execution_id
  • function_name
  • original_completion_time
  • delayed_completion_time (calculated as original_completion_time + delay_ms)

Table Schema

async_functions
Column Name Type Description
execution_id PK int Unique identifier for each function execution
function_name varchar Name of the async function
original_completion_time int Original completion time in milliseconds
delay_ms int Additional delay in milliseconds to be added
Primary Key: execution_id
Note: Each row represents an async function execution with its timing details

Input & Output

Example 1 — Basic Promise Delay
Input Table:
execution_id function_name original_completion_time delay_ms
1 fetchData 100 50
2 saveUser 200 100
3 sendEmail 80 40
Output:
execution_id function_name original_completion_time delayed_completion_time
3 sendEmail 80 120
1 fetchData 100 150
2 saveUser 200 300
💡 Note:

Each function gets its delay added to the original completion time. Results are ordered by delayed completion time: sendEmail (80+40=120), fetchData (100+50=150), saveUser (200+100=300).

Example 2 — Zero Delay Case
Input Table:
execution_id function_name original_completion_time delay_ms
1 quickTask 50 0
2 slowTask 300 25
Output:
execution_id function_name original_completion_time delayed_completion_time
1 quickTask 50 50
2 slowTask 300 325
💡 Note:

When delay_ms is 0, the delayed completion time equals the original completion time. quickTask has no additional delay (50+0=50), while slowTask gets 25ms delay (300+25=325).

Constraints

  • 1 ≤ execution_id ≤ 1000
  • 1 ≤ original_completion_time ≤ 10000
  • 0 ≤ delay_ms ≤ 5000
  • function_name is a non-empty string

Visualization

Tap to expand
Delay the Resolution of Each Promise INPUT id time delay 1 100ms 50ms 2 200ms 100ms 3 150ms 25ms Promise Objects P1 100ms P2 200ms P3 150ms Async functions with completion times ALGORITHM STEPS 1 Wrap Each Promise Create delay wrapper setTimeout(resolve, delay) 2 Calculate New Time delayed = time + delay 100 + 50 = 150ms 3 Process All Promises Map through records Promise.all(delayed) 4 Sort by Completion Order by delayed time .sort((a,b) => a-b) Delayed Calculations: P1: 100+50 = 150ms P2: 200+100 = 300ms P3: 150+25 = 175ms FINAL RESULT id original delayed 1 100ms 150ms 3 150ms 175ms 2 200ms 300ms Output Order [P1, P3, P2] Timeline 0ms 300ms P1 150ms P3 175ms P2 300ms Sorted by delayed time Key Insight: Each promise's resolution is delayed by wrapping it with setTimeout. The final completion time equals original_time + delay_value. Results are sorted by this delayed completion time. Use Promise.all() to process all delayed promises concurrently, then sort the results. TutorialsPoint - Delay the Resolution of Each Promise | Optimal Solution
Asked in
Microsoft 28 Netflix 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 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