First of all, there are some issues with console.log in Google Chrome not functioning as expected. This is not the case as I am working in VSCode.
We begin with two
asynccalls to the server.promise_a = fetch(url) promise_b = fetch(url)Since
fetchresults are also promises,.json()will needed to be called on each item. The helper function process will be used, as suggested by a Stackoverflow user -- sorry lost the link.
let promiseResults = []
let process = prom => {
prom.then(data => {
promiseResults.push(data);
});
};
Promise.allis called. The resulting array is passed to.thenwhere forEach calls process on item.json() each iteration and fulfilled promises are pushed topromiseResults.
Promise.all([promise_a, promise_b])
.then(responseArr => {
responseArr.forEach(item => {
process(item.json());
});
})
No argument is given to the final .then block because
promiseResultsare in the outer scope.console.logshow confusing results..then(() => { console.log(promiseResults); // correct results console.log(promiseResults[0]); // undefined ?!? })
Any help will be greatly appreciated.