i'm trying to understand Promise.all in a map function using async await but i seem to returning promises which are pending, i don't understand why
Here is the working code when i have a .then to resolve my promise
const arr = [1, 2, 3, 4, 5];
const results = Promise.all(arr.map( item => {
return item + 1;
}));
results.then(result=> console.log(result))
which logs as expected = [ 2, 3, 4, 5, 6 ]
now to implement the function using async-await i know i need to wrap the function in an async function with the keyword async and await for promise.all
const results = async () => await Promise.all(arr.map(async (item) => {
return item + 1;
}));
console.log(results())
but i always seem to log Promise { <pending> }
I don't understand what im doing wrong
console.log(await results())
– Rashomonasync/await
. But basically the whole purpose ofasync
is to force a function to return a promise, so it can hardly be surprising that its return value is indeed a promise! – Robin Zigmond