I run a forEach loop in an array and make two calls that return promises, and I want to populate the say this.options object and then do other things with it. Right now I am encountering the async problem if I use the following code sample and I enter the then function first.
$.when.apply($, someArray.map(function(item) { return $.ajax({...}).then(function(data){...}); })).then(function() {
This is the working code below, but it only works for the first element in the array, because I call the resulting function in the .then response. I want to do all the first selection for all elements of the array, and then call the resulting function to do something.
array.forEach(function(element) { return developer.getResources(element) .then((data) = > { name = data.items[0]; return developer.getResourceContent(element, file); }) .then((response) = > { fileContent = atob(response.content); self.files.push({ fileName: fileName, fileType: fileType, content: fileContent }); self.resultingFunction(self.files) }).catch ((error) = > { console.log('Error: ', error); }) });
How to populate the self.files object after the forEach loop completes, and then call the resulting function with the file object?
javascript arrays promise foreach es6-promise
rond
source share