Callback after all asynchronous forEach callbacks are completed

后端 未结 13 2275
谎友^
谎友^ 2020-11-22 12:56

As the title suggests. How do I do this?

I want to call whenAllDone() after the forEach-loop has gone through each element and done some asynchronous p

13条回答
  •  借酒劲吻你
    2020-11-22 13:17

     var counter = 0;
     var listArray = [0, 1, 2, 3, 4];
     function callBack() {
         if (listArray.length === counter) {
             console.log('All Done')
         }
     };
     listArray.forEach(function(element){
         console.log(element);
         counter = counter + 1;
         callBack();
     });
    

提交回复
热议问题