admin管理员组文章数量:1410697
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
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
- you cannot use async with map like that. sorry but i'm on phone so.. i might give you a solution tomorrow. i stumbled across this issue before and fixed it – GottZ Commented May 12, 2019 at 20:43
-
1
You have to await the results
console.log(await results())
– Rashomon Commented May 12, 2019 at 20:48 -
you're not actually doing anything asynchronous here, so there should be no need for promises or
async/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 Commented May 12, 2019 at 20:53
3 Answers
Reset to default 5Using asnyc
/await
doesn't make the results
function synchronous. (It's literally tagged as async
!) So it returns a promise, and you have to await
that before logging the values:
const arr = [1, 2, 3];
const results = Promise.all(
arr.map(async item => item + 1)
);
(async () => {
console.log(await results)
})();
What you are doing is assigning an async function to results, thats not how await for the asynchronous function works. You need to assign a promise to results and to avoid then
callback chaining we can use async/await
to get the response.
const arr = [1, 2, 3];
const results = Promise.all(arr.map(async (item) => {
return item + 1;
}));
async function output () {
console.log(await results);
}
output();
Like other answers have mentioned, async returns a promise. Thats the reason you are getting
Promise { <pending> }
Irrespective of whether what you are doing makes sense, you could just use then on this promise that was returned like so
const results = async () =>
await Promise.all(
[1, 2, 3, 4, 5].map(async item => {
return item + 1;
})
);
results().then(e => console.log(e));
本文标签: javascriptasync await promiseall map not resolving promisesStack Overflow
版权声明:本文标题:javascript - async await promise.all map not resolving promises - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744252427a2597313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论