Implement createDeferred() in JavaScript

intermediate
By FrontendAtlas Editorial · Updated Oct 21, 2026
Implement createDeferred() in JavaScript. Your function should return { promise, resolve, reject } so async tests can control exactly when a Promise fulfills or rejects. The challenge includes starter code, tests for resolve/reject behavior, promise adoption, pending state, first-settlement-wins edge cases, and interview follow-ups.

What you'll practice:

  • Creating createDeferred() with { promise, resolve, reject } controls
  • Making async tests deterministic without real timers
  • Handling resolve(value), reject(error), Promise adoption, pending state, and first-settlement-wins behavior

Expected behavior

Your createDeferred() implementation should:

  • Return { promise, resolve, reject }
  • Fulfill the promise when resolve(value) is called
  • Reject the promise with the exact reason when reject(error) is called
  • Adopt another Promise when resolve(Promise.resolve(value)) is used
  • Remain pending until resolve or reject is called
  • Use normal Promise semantics so only the first settlement wins and callbacks run in a later microtask

Returns

{ promise: Promise, resolve: Function, reject: Function } — The createDeferred() controls object: a Promise plus resolve(value) and reject(error) functions for settling it later.

Solution idea

Interview answer: the base prompt is to create one Promise and capture its resolve and reject functions from the Promise constructor. Return those controls with the promise so tests can decide when async work finishes. TypeScript typing, timeout safeguards, cancellation, and production-safety concerns are follow-ups, not part of the minimal createDeferred() contract.

Examples
// Example: timeline
// At t=0
const d = createDeferred();
const result = d.promise.then((value) => `loaded:${value}`);

// At t=50
// d.promise is still pending because neither resolve nor reject has been called.

// At t=100
d.resolve('user');

// After the next microtask
await result; // 'loaded:user'

// Promise adoption
const adopted = createDeferred();
adopted.resolve(Promise.resolve('ok'));
await adopted.promise; // 'ok'

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Waymo interview account. Waymo frontend interview questions

Reported in a public Shopee interview account. Shopee frontend interview questions