Create Deferred
EasyPromises & Async10 min4 tests
Implement createDeferred() that returns an externally controllable promise.
Requirements
- Return an object with
{ promise, resolve, reject }. promiseis a standard Promise.- Calling
resolve(val)resolves the promise withval. - Calling
reject(err)rejects the promise witherr.
Example
const d = createDeferred();
setTimeout(() => d.resolve('done'), 100);
const result = await d.promise; // 'done'Hints (3)
- Create a new Promise and capture its resolve and reject callbacks.
- Store resolve and reject in variables declared outside the Promise constructor.
- Return an object containing all three: promise, resolve, reject.
Topics
- Promises & Async
Asked at
Google · Microsoft · Amazon
