My problem is that I donβt know how to know when the dynamic promises grid resolves all the promises.
Here is an example:
var promiseArray = []; promiseArray.push(new Promise(){}); promiseArray.push(new Promise(){}); Promise.all(promiseArray).then(function(){
I have a problem. The behavior of Promise.all will be executed when the previous 2 promises are resolved, BUT, before these 2 promises are resolved, the third promise is added, and this new one will not be taken into account.
So, I need something like: "Hello, Promise.all , you have a dynamic array to check." How can I do it?
Remember, this is just an example. I know that I can move the Promise.all line to the last line, but in fact new promises are added dynamically when other promises are resolved, and new promises can also add new promises, so this is really a dynamic array.
The real use case that I have looks something like this:
- I use the Twitter API to check for new tweets (using Search Api).
- In case I found new tweets, I add them to MongoDB (here we have promises).
- In case these new tweets are associated with a user that I do not have in my MongoDB (here we have new promises because I have to go to MongoDB to check if I have this user), we go to the Twitter API for user information (more promises), and we add new users to MongoDB (yes, more promises).
- Then I go to MongoDB to insert new values ββto associate new tweets with these new users (more promises! Wiii!).
- When all requests in MongoDB are allowed (all selections, updates, inserts), close the MongoDB connection.
Another tough example:
var allPromises = []; allPromises.push(new Promise(function(done, fail){ mongoDB.connect(function(error){ //Because mongoDB works with 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 will be take in account // bla bla bla if (somethingHappens) { allPromises.push(new Promise(function(done, fail){ //This promise never will be 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 will be 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 will be take in account // bla bla bla if (somethingHappens) { allPromises.push(new Promise(function(done, fail){ //This promise never will be 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 will be take in account // bla bla bla })); } }); } })); } }); } }); }); })); Promise.all(allPromises).then(function(){ // Soooo, all work is done! mongodb.close()! });
So now, an example of beauty. We need to call the showAllTheInformation function when showAllTheInformation last (we don't know which last) promise. How do you do this?:
var name = 'anonimus'; var date = 'we do 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's another example with great context: https://jsfiddle.net/f0a1s79o/2/
javascript promise ecmascript-6
Broda noel
source share