How to know when all Promises are Resolved in a dynamic "iterable" parameter?
<p>My problem is that I don't know how to know when a dynamic promise array has all the promises resolved.</p>

<p>Here an example:</p>

<pre><code>var promiseArray = [];
promiseArray.push(new Promise(){/*blablabla*/});
promiseArray.push(new Promise(){/*blablabla*/});
Promise.all(promiseArray).then(function(){
    // This will be executen when those 2 promises are solved.
});
promiseArray.push(new Promise(){/*blablabla*/});
</code></pre>

<p>I have a problem here. The <code>Promise.all</code> behavior will be executed when the previous 2 promises are solved, BUT, before those 2 promises were solved, a third promise where added and this new one won't be take in account.</p>

<p>So, what I need, is say something like: "Hey <code>Promise.all</code>, you have a dynamic array to check". How can I do it?</p>

<p>Remember that this is just an example. I know I can move the line <code>Promise.all</code> to the last line, but actually the new promises are added dynamically when another promises are solved, and the new promises could add new promises as well, so, it's a really dynamic array.</p>

<p>The real use case that I have is something like this:</p>

<ol>
<li>I use Twitter API to check if there are new Tweets (using the Search Api).</li>
<li>In case I found new Tweets, I add it to a MongoDB (here we have Promises).</li>
<li>In case that those new Tweets are related to 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 that user), we go to Twitter API to get user info (more promise) and we add those new users to MongoDB (yes, more promises).</li>
<li>Then, I go to MongoDB to insert new values to associate the new tweets with those new users (more promises! wiii!).</li>
<li>When all the queries to MongoDB are Resolved (all the selects, updates, inserts), close the MongoDB connection.</li>
</ol>

<p>Another hard example:</p>

<pre><code>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()!
});
</code></pre>

<p>So, now, a beauty example. We need to call the <code>showAllTheInformation</code> function when the last (we don't know which is the last) promise is called. How do you do it?:</p>

<pre><code>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);
}
</code></pre>

<p>here another example with more context:
<a href="https://jsfiddle.net/f0a1s79o/2/" rel="noreferrer">https://jsfiddle.net/f0a1s79o/2/</a></p>