javascript - How to know when all Promises are Resolved in a dynamic "iterable" parameter? -


my problem don't know how know when dynamic promise array has promises resolved.

here example:

var promisearray = []; promisearray.push(new promise(){/*blablabla*/}); promisearray.push(new promise(){/*blablabla*/}); promise.all(promisearray).then(function(){     // executen when 2 promises solved. }); promisearray.push(new promise(){/*blablabla*/}); 

i have problem here. promise.all behavior executed when previous 2 promises solved, but, before 2 promises solved, third promise added , new 1 won't take in account.

so, need, like: "hey promise.all, have dynamic array check". how can it?

remember example. know can move line promise.all last line, new promises added dynamically when promises solved, , new promises add new promises well, so, it's dynamic array.

the real use case have this:

  1. i use twitter api check if there new tweets (using search api).
  2. in case found new tweets, add mongodb (here have promises).
  3. in case new tweets related user not have in mongodb (here have new promises because have go mongodb check if have user), go twitter api user info (more promise) , add new users mongodb (yes, more promises).
  4. then, go mongodb insert new values associate new tweets new users (more promises! wiii!).
  5. when queries mongodb resolved (all selects, updates, inserts), close mongodb connection.

another hard example:

var allpromises = [];  allpromises.push(new promise(function(done, fail){     mongodb.connect(function(error){         //because mongodb works callbacks instead of promises         if(error)             fail();         else             ajax.get('/whatever').then(function(){                 if (somethinghappens) {                     allpromises.push(new promise(function(done, fail){ //this promise never take in account                         // bla bla bla                         if (somethinghappens) {                             allpromises.push(new promise(function(done, fail){ //this promise never take in account                                 // bla bla bla                             }));                         } else {                             ajax.get('/whatever/2').then(function(){                                 if (somethinghappens) {                                     allpromises.push(new promise(function(done, fail){ //this promise never take in account                                         // bla bla bla                                     }));                                 }                             });                         }                     }));                 } else {                     ajax.get('/whatever/2').then(function(){                         if (somethinghappens) {                             allpromises.push(new promise(function(done, fail){ //this promise never take in account                                 // bla bla bla                                     if (somethinghappens) {                                         allpromises.push(new promise(function(done, fail){ //this promise never take in account                                             // bla bla bla                                         }));                                     } else {                                         ajax.get('/whatever/2').then(function(){                                             if (somethinghappens) {                                                 allpromises.push(new promise(function(done, fail){ //this promise never take in account                                                     // bla bla bla                                                 }));                                             }                                         });                                     }                             }));                         }                     });                 }             });     }); }));  promise.all(allpromises).then(function(){     // soooo, work done!     mongodb.close()! }); 

so, now, beauty example. need call showalltheinformation function when last (we don't know last) promise called. how do it?:

var name = 'anonimus'; var date = 'we not know';  function userclikonlogin() {     $http.get('/login/user/password').then(function(data){         if (data.logguedok) {             $http.get('/checkifisadmin').then(function(data){                 if (data.yesheisanadmin) {                     $http.get('/getthenameoftheuser').then(function(data){                         if(data.userhasname) {                             $http.get('/getcurrentdate').then(function(data){                                 currentdate = data.thenewcurrentdate;                             });                         }                     });                 }             });         }     }); }  function showalltheinformation() {     alert('hi ' + name + ' today is:' + date); } 

here example more context: https://jsfiddle.net/f0a1s79o/2/

there's no way out. have put promises in array before calling promise.all in it. in example presented, that's simple moving last line top.

in case asynchronously filling array, should promise array, , use .then(promise.all.bind(promise)). if don't know when stop adding promises, impossible anyway might never resolved @ all.


regarding "beauty example", want learn magic of chaining. previosly said in comments, have return promise every function in doing asynchronous. indeed, add missing returns:

function userclikonlogin() {     return $http.get('/login/user/password').then(function(data){ //  ^^^^^^         if (data.logguedok) {             return $http.get('/checkifisadmin').then(function(data){ //          ^^^^^^                 if (data.yesheisanadmin) {                     return $http.get('/getthenameoftheuser').then(function(data){ //                  ^^^^^^                         if(data.userhasname) {                             return $http.get('/getcurrentdate').then(function(data){ //                          ^^^^^^                                 currentdate = data.thenewcurrentdate;                             });                         }                     });                 }             });         }     }); }  userclikonlogin().then(function showalltheinformation() { //               ^^^^^ can chain onto it!     alert('hi ' + name + ' today is:' + date); }); 

there no array of promises here dynamically grows, it's every function returning promise (asynchronous) result of things does.


Comments