FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Promise Merge

20mins

Create a function promiseMerge(p1, p2) that takes two promises and resolves into a single value containing both results.

The function should:

  • Wait until both promises have resolved.
  • Return a promise that resolves with an array [result1, result2].
  • If either promise rejects, the returned promise should reject with the error.

Examples

const p1 = Promise.resolve(1); const p2 = Promise.resolve(2); promiseMerge(p1, p2).then(console.log); // [1, 2] const p3 = Promise.reject('error'); promiseMerge(p1, p3).catch(console.error); // 'error'

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests