Created
December 29, 2024 16:50
-
-
Save PetukhovArt/60a31469cf803ed0bfe448daf258caea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetchMock = (delay) => { | |
return new Promise((resolve) => setTimeout(() => resolve(delay), delay)); | |
}; | |
const promiseAll = async (promises) => { | |
return new Promise((resolve, reject) => { | |
const results = []; | |
promises.forEach((promise) => { | |
promise | |
.then((res) => { | |
results.push(res); | |
if (results.length === promises.length) { | |
resolve(results); | |
} | |
}) | |
.catch((e) => { | |
reject(e); | |
}); | |
}); | |
}); | |
}; | |
promiseAll([fetchMock(800), fetchMock(500)]) | |
.then((data) => console.log(data)) | |
.catch((error) => console.log([error])); | |
// [800, 500] | |
promiseAll([fetchMock(200), fetchMock(100), Promise.reject("error")]) | |
.then((data) => console.log(data)) | |
.catch((error) => console.log([error])); | |
// ['error'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment