FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Promise Resolve

20mins

Implement a function promiseResolve(value) that mimics the behavior of Promise.resolve.

The function should:

  • If value is already a Promise, return it as is.
  • If value is a thenable (an object with a then method), adopt its eventual state.
  • Otherwise, wrap the value in a resolved Promise.

Examples

promiseResolve(42).then(console.log); // 42 promiseResolve({ then: res => res("hello") }).then(console.log); // "hello" const p = Promise.resolve("world"); promiseResolve(p).then(console.log); // "world"

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests