I want to return the consolidated results from calling many async/await
functions. Consider my (wrong) pseudocode below
const getRemoteData = async function(uri) {
// get result from remote server via XMLHttpRequest
return result;
}
const finalResult {};
const result1 = getRemoteData('http://…');
result1.forEach(async record => {
// each record has a key for another URI
const result2 = await getRemoteData(record["uri"]);
// result2 has a key for yet another URI
const result3 = await getRemoteData(result2["uri"]);
finalResult[ result2["uri"] ] = result3;
})
console.log(finalResult);
Of course, since console.log(finalResult)
is called before all the interim calls in the forEach
loop have completed, finalResult
is empty. How do I return finalResult
after all the entries in result1
have been processed? I am using the latest version of nodejs
and, if possible, would rather not use any other Promise
library. From what I understand, the new async/await
capabilities should enable me to accomplish what I want.