FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Promise.withResolvers

25mins

Implement a function promiseWithResolvers() that creates a new Promise along with its resolver functions.

The function should:

  • Return an object containing:
    • promise: the new promise instance.
    • resolve: a function to resolve the promise.
    • reject: a function to reject the promise.
  • Ensure that calling resolve or reject changes the state of the returned promise.

Examples

const { promise, resolve, reject } = promiseWithResolvers(); setTimeout(() => resolve("done"), 1000); promise.then(console.log); // "done"

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests