← JavaScript Coding

66. Promise Methods

easy
Asked at
ClickUp
Facebook
LinkedIn
ClickUp, Facebook, LinkedIn

Problem statement

Extend the Promise object in JavaScript by implementing custom methods that mimic the functionality of existing Promise methods. Specifically, you are required to create the following methods:

  • Promise.myRace: This method should mimic the behavior of Promise.race, returning a promise that resolves or rejects as soon as one of the promises in the input array does, reflecting that promise’s outcome.
  • Promise.myAny: Similar to Promise.any, this method should return a promise that resolves as soon as any one of the input promises resolves, with the value of the resolved promise. If all input promises are rejected, it should reject with an aggregate error.
  • Promise.myAll: This method should emulate Promise.all, returning a promise that resolves when all of the input promises have resolved, with an array of the results, or rejects as soon as one of the input promises rejects.
  • Promise.myAllSettled: Inspired by Promise.allSettled, this method should return a promise that resolves after all input promises have settled (resolved or rejected), with an array of objects that each describe the outcome of each promise.

For example, using Promise.myRace, you can race multiple promises against each other and act upon the first to settle, whether it resolves or rejects:

Promise.myRace([Promise.resolve('First'), Promise.reject('Second'), Promise.resolve('Third')])
  .then(value => console.log(value))
  .catch(error => console.error(error));
// Expected output: "First"

Implementing these methods will give you deeper insights into the workings of promises in JavaScript, enhancing your understanding of asynchronous programming patterns.

Requirements & constraints

  • →Input must be an array of Promises for all methods (myRace, myAny, myAll, myAllSettled).
  • →Each method returns a new Promise.
  • →Promise.myRace: Resolves or rejects with the outcome of the first settled Promise.
  • →Promise.myAny: Resolves with the first resolved Promise; rejects with "all promises rejected" if all Promises reject.
  • →Promise.myAll: Resolves with an array of resolved values if all Promises resolve; rejects with the first rejection reason if any Promise rejects.
  • →Promise.myAllSettled: Resolves with an array of objects detailing the outcomes of all Promises, regardless of their resolution or rejection.

How to approach 66. Promise Methods

The strategy an interviewer expects you to reach for.

Implementing Promise.race/all/any-style helpers is an async fundamentals check — settlement rules, not syntax sugar. Naive move Await in a serial for-loop. That changes both timing and failure semantics. Where it breaks race: first settlement. all: fail-fast + ordered results. Mixing those rules is the usual bug. The fix For race, attach then/catch to each input onto one outer promise. For all, write by index and reject on first failure. Spell the rule before coding each method. Remember: Say the settlement rule first; the code is just wiring. Say this in the interview: I'd implement each Promise helper by stating its settlement rule first — race is first settle, all is fail-fast with indexed results — then wire the handlers.

Premium

The full solution is part of HelloFrontend Pro

The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.

  • Runnable editor with the hidden test suite
  • Progressive hints that unlock as you get stuck
  • Senior-level reference solution with a line-by-line walkthrough
Unlock the full solution →

Already a member? Log in to open the workspace.

More JS Coding questions

View all JS Coding →
JS CodingMedium

5. Throttle

JS CodingMedium

13. Deep Clone

JS CodingMedium

14. Event Emitter

JS CodingEasy

19. Memoize I

JS CodingMedium

86. API Retry

JS CodingEasy

mul(a)(b)(c)

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub
  • ClickUp interview
  • Facebook interview
  • LinkedIn interview