I am attempting to write an async function that makes multiple fetches, waits for all to complete, then returns the JSON results as an array. This is what I have so far:
file1.js
const searchResults = await Api.search(searchText);
api.js
async function search(searchText) {
return util.executeFetchAll([
`/searchA/${searchText}`,
`/searchB/${searchText}`,
`/searchC/${searchText}`
]);
}
util.js
async function executeFetchAll(urls) {
const promises = urls.map(url => fetch(url));
const responses = await Promise.all(promises);
debugger;
}
When execution pauses at the debugger and I inspect responses
using Chrome's dev tools, it is correctly an array of 3 Response
objects, but if I inspect responses[0].json()
, it weirdly returns in the console a Promise {<pending>}
object.
What am I missing? I'm awaiting Promise.all
, which should mean all promises resolve before my debugger line. So why is the json()
method weirdly showing a Promise object in a pending state?
Thanks.