admin管理员组文章数量:1178543
I am trying to create a Promise.all
with an array of items. So if I create it like this it works fine
Promise.all([
Query.getStuff(items[0]),
Query.getStuff(items[1])
]).then(result => console.log(result))
If I try to create the Promise.all
like this, it doesn't work
Promise.all([
items.map(item => Query.getStuff(item))
]).then(result => console.log(result))
The then
block is run before the Query.getStuff(item)
. What am I missing?
I am trying to create a Promise.all
with an array of items. So if I create it like this it works fine
Promise.all([
Query.getStuff(items[0]),
Query.getStuff(items[1])
]).then(result => console.log(result))
If I try to create the Promise.all
like this, it doesn't work
Promise.all([
items.map(item => Query.getStuff(item))
]).then(result => console.log(result))
The then
block is run before the Query.getStuff(item)
. What am I missing?
1 Answer
Reset to default 39You should be writing
Promise.all(items.map(...))
instead of
Promise.all([ items.map(...) ])
Array#map
returns an array, which means that the way you wrote your code originally, you were actually passing a multidimensional array to Promise.all
— as in [ [promise1, promise2, ...] ]
— instead of the expected one-dimensional version [promise1, promise2, ...]
.
Revised Code:
Promise.all(
items.map(item => Query.getStuff(item))
).then(result => console.log(result))
本文标签: How to use Arraymap within Promiseall in JavaScriptStack Overflow
版权声明:本文标题:How to use `Array#map` within `Promise.all` in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738080948a2060411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论