Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function difficultMathCalculation(num)
- {
- return new Promise((resolve, _) => setTimeout(() => resolve(num * num), 5000));
- }
- function bestResult(results)
- {
- return Math.max(...results);
- }
- function doSomeStuff(array) {
- const promises = [];
- array.forEach(elem => {
- promises.push(difficultMathCalculation(elem));
- });
- return promises;
- };
- function getBestResult(array) {
- let promises = doSomeStuff(array);
- return new Promise((resolve, _) => {
- let allResults = [];
- Promise.allSettled(promises).then(results => {
- results.forEach(result => allResults.push(result.value));
- resolve(bestResult(allResults));
- });
- });
- }
- const array = [3, 2, 1, 9, 6, 5, 7, 8, 4];
- getBestResult(array).then(best => console.log(best));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement