Advertisement
Guest User

Untitled

a guest
Jul 10th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function difficultMathCalculation(num)
  3. {
  4.     return new Promise((resolve, _) => setTimeout(() => resolve(num * num), 5000));
  5. }
  6.  
  7. function bestResult(results)
  8. {
  9.     return Math.max(...results);
  10. }
  11.  
  12. function doSomeStuff(array) {
  13.     const promises = [];
  14.     array.forEach(elem => {
  15.         promises.push(difficultMathCalculation(elem));
  16.     });
  17.  
  18.     return promises;
  19. };
  20.  
  21. function getBestResult(array) {
  22.     let promises = doSomeStuff(array);
  23.     return new Promise((resolve, _) => {
  24.         let allResults = [];
  25.         Promise.allSettled(promises).then(results => {
  26.             results.forEach(result => allResults.push(result.value));
  27.             resolve(bestResult(allResults));
  28.         });
  29.     });
  30. }
  31.  
  32. const array = [3, 2, 1, 9, 6, 5, 7, 8, 4];
  33. getBestResult(array).then(best => console.log(best));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement