Create Deferred

EasyPromises & Async10 min4 tests

Implement createDeferred() that returns an externally controllable promise.

Requirements

  • Return an object with { promise, resolve, reject }.
  • promise is a standard Promise.
  • Calling resolve(val) resolves the promise with val.
  • Calling reject(err) rejects the promise with err.

Example

const d = createDeferred();
setTimeout(() => d.resolve('done'), 100);
const result = await d.promise; // 'done'
Hints (3)
  1. Create a new Promise and capture its resolve and reject callbacks.
  2. Store resolve and reject in variables declared outside the Promise constructor.
  3. Return an object containing all three: promise, resolve, reject.

Topics

  • Promises & Async

Asked at

Google · Microsoft · Amazon

Join Us
blur