kbo25

await-problem

Jun 11th, 2019
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const functions = [
  2.     (callback) => {
  3.         setTimeout (() => {
  4.             console.log(4);
  5.             callback && callback();
  6.         }, 4000);
  7.     },
  8.     (callback) => {
  9.         setTimeout (() => {
  10.             console.log(2);
  11.             callback && callback();
  12.         }, 2000);
  13.     }
  14. ]
  15.  
  16. const wait = async (arr, cb) => {
  17.     // currently 'all done' is displayed before 2, 4
  18.     await new Promise((resolve, reject) => {
  19.         for (let func of arr) {
  20.             func();
  21.         }
  22.         resolve();
  23.     });
  24.     cb();
  25. }
  26.  
  27. const callback = () => {
  28.     console.log('all done');
  29. }
  30.  
  31. wait(functions, callback);
Advertisement
Add Comment
Please, Sign In to add comment