Js.myPromiseRace

const myPromiseRace = (promises) => {
    return new Promise((resolve) => {
        for(let i = 0; i < promises.length; i++){
            promises[i].then((value) => {
                resolve(value); // if resolved here finish all other
            })
        }
    });
}

// lets test it

const p1 = new Promise((resolve) => {
    setTimeout(resolve, 1000, 'one');
});

const p2 = new Promise((resolve) => {
    setTimeout(resolve, 2000, 'two');
});

const p3 = new Promise((resolve) => {
    setTimeout(resolve, 3000, 'two');
});

output

one
This entry was posted in Без рубрики. Bookmark the permalink.