Problem, as an interviewer would state it:
"Write a sleep(ms) function I can await to pause execution for a given number of milliseconds, and a delay(value, ms) function that resolves with a given value after that delay. Show me you understand what actually makes this 'pause' different from a language like Python's time.sleep()."
Examples:
await sleep(100); // pauses ~100ms, resolves with undefined
await delay("done", 100); // pauses ~100ms, resolves with "done"
Clarifying questions expected:
- Does "pause" mean genuinely blocking everything else on the page, or just pausing the calling async function while other code keeps running? (This is the crux of the whole problem.)
- Is a rejection path ever needed (a cancellable sleep), or is resolve-only fine for this version?
- Is sub-millisecond precision expected, or is "close to
ms" acceptable? (Real timers are never exact.)
Code / implementation expected: Yes — both functions, plus a real, direct demonstration that the implementation does not block the event loop, since that is the entire point of the question.