How to use promise in forEach loop of array to populate an object
<p>I am running a forEach loop on an array and making two calls which return promises, and I want to populate an object say <code>this.options</code>, and then do other stuff with it. Right now I am running into the async issue if i use the following code sample and i get into the then function first.</p>

<pre><code>$.when.apply($, someArray.map(function(item) {
    return $.ajax({...}).then(function(data){...});
})).then(function() {
    // all ajax calls done now
});
</code></pre>

<p>This is working code below, but it only works for the first element in the array, because I call the resulting function in the <code>.then</code> of the response. I want to do all the fetch first for all elements of the array and then call the resulting function to do something.</p>

<pre><code>array.forEach(function(element) {
    return developer.getResources(element)
        .then((data) = &gt; {
            name = data.items[0];
            return developer.getResourceContent(element, file);
        })
        .then((response) = &gt; {
            fileContent = atob(response.content);
            self.files.push({
                fileName: fileName,
                fileType: fileType,
                content: fileContent
            });
            self.resultingFunction(self.files)
        }).catch ((error) = &gt; {
            console.log('Error: ', error);
        })
});
</code></pre>

<p>How do i populate the <code>self.files</code> object after the forEach loop is complete, and then call the resulting function with the files object? </p>