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
async
calls to the server.promise_a = fetch(url) promise_b = fetch(url)
Since
fetch
results 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.all
is called. The resulting array is passed to.then
where 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
promiseResults
are in the outer scope.console.log
show confusing results..then(() => { console.log(promiseResults); // correct results console.log(promiseResults[0]); // undefined ?!? })
Any help will be greatly appreciated.