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
21 stars 4 forks source link

Promise.race #37

Closed ashutoshbarthwal closed 4 months ago

ashutoshbarthwal commented 4 months ago

Info

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

Question

According to the MDN documentation, the Promise.race() method is used to return a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise. This feature is particularly useful when you have multiple asynchronous operations and only need the result of the first one to complete.

For e.g:

const promise1 = new Promise(resolve => setTimeout(() => resolve('First'), 1000));
const promise2 = new Promise(resolve => setTimeout(() => resolve('Second'), 500));
const promise3 = new Promise((resolve, reject) => setTimeout(() => reject(new Error('Third')), 2000));

Promise.race([promise1, promise2, promise3])
    .then(result => {
        console.log('First promise to resolve:', result);
        // This will print "Second"
    })
    .catch(error => {
        console.error('First promise to reject:', error.message);
        // This will not be reached as promise2 resolves first
    });

Your task is to implement your own version of the Promise.race() method in JavaScript. This function should take an array of promises as input and return a single Promise that resolves or rejects as soon as the first promise in the array resolves or rejects, providing the value or reason from that promise. Ensure your implementation correctly handles both resolved and rejected promises, maintaining the order of completion.

Template

index.js


export function promiseRace(promises) {

}

index.test.js

import { promiseRace } from "./";

describe('promiseRace', () => {
  test('Resolves with the first resolved promise', async () => {
    const promise1 = new Promise(resolve => setTimeout(() => resolve('First'), 1000));
    const promise2 = new Promise(resolve => setTimeout(() => resolve('Second'), 500));
    const promise3 = new Promise((resolve, reject) => setTimeout(() => reject(new Error('Third')), 2000));

    const result = await promiseRace([promise1, promise2, promise3]);
    expect(result).toBe('Second');
  });

  test('Rejects with the first rejected promise', async () => {
    const promise1 = new Promise(resolve => setTimeout(() => resolve('First'), 1000));
    const promise2 = new Promise(resolve => setTimeout(() => resolve('Second'), 1500));
    const promise3 = new Promise((resolve, reject) => setTimeout(() => reject(new Error('Third')), 500));

    await expect(promiseRace([promise1, promise2, promise3])).rejects.toThrow('Third');
  });
});
github-actions[bot] commented 4 months ago

39 - Pull Request updated.