jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
27 stars 4 forks source link

Promise.all #35

Closed jsartisan closed 6 months ago

jsartisan commented 6 months ago

Info

difficulty: hard
title: Promise.all
template: javascript
tags: javascript

Question

According to the MDN documentation, the Promise.all() method is designed to handle an iterable of promises as input and produces a single Promise. This Promise resolves into an array containing the results of all the input promises. This functionality proves valuable when managing multiple asynchronous operations that must be completed before advancing to subsequent tasks. Here's a practical example to illustrate its usage:

const promise1 = new Promise(resolve => setTimeout(() => resolve('Apple'), 500));
const promise2 = 'Banana';
const promise3 = new Promise((resolve, reject) => {
  setTimeout(() => {
      resolve("Orange");
  }, 1000);
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
}).catch(error => {
  console.error('At least one promise rejected:', error.message);
});

// The above code will give output: ["Apple", "Banana", "Orange"]

Implement your own version of the Promise.all() method in JavaScript. This function should take an array of promises as input and return a single Promise that resolves to an array containing the results of all input promises. Remember that Promise.all() should reject if any of the input promises are rejected, with the first rejection message or error. Ensure your implementation handles asynchronous operations correctly and maintains the order of the input promises.

Template

index.js

export function promiseAll(promises) {

}

index.test.js

import { promiseAll } from './';

describe('promiseAll', () => {
  test('Resolves all promises correctly', async () => {
    const promise1 = Promise.resolve('Apple');
    const promise2 = 'Banana';
    const promise3 = new Promise(resolve => setTimeout(() => resolve('Cherry'), 500));

    const results = await promiseAll([promise1, promise2, promise3]);
    expect(results).toEqual(['Apple', 'Banana', 'Cherry']);
  });

  test('Resolves empty array', async () => {
    const results = await promiseAll([]);
    expect(results).toEqual([]);
  });

  test('Rejects with error message when any promise rejects', async () => {
    const promise1 = Promise.resolve('Apple');
    const promise2 = Promise.reject(new Error('Error occurred'));
    const promise3 = new Promise(resolve => setTimeout(() => resolve('Cherry'), 500));

    await expect(promiseAll([promise1, promise2, promise3])).rejects.toThrow('Error occurred');
  });

  test('Maintains the order of promises', async () => {
    const promise1 = new Promise(resolve => setTimeout(() => resolve('First'), 1000));
    const promise2 = new Promise(resolve => setTimeout(() => resolve('Second'), 500));
    const promise3 = new Promise(resolve => setTimeout(() => resolve('Third'), 2000));

    const results = await promiseAll([promise1, promise2, promise3]);
    expect(results).toEqual(['First', 'Second', 'Third']);
  });
});
github-actions[bot] commented 6 months ago

36 - Pull Request updated.