相关疑难解决方法(0)

将数组拆分为块

假设我有一个Javascript数组,如下所示:

["Element 1","Element 2","Element 3",...]; // with close to a hundred elements.
Run Code Online (Sandbox Code Playgroud)

什么方法适合将数组块(拆分)到许多较小的数组中,比如最多10个元素?

javascript arrays split

442
推荐指数
37
解决办法
45万
查看次数

等待 VS Promise.all

这有什么区别吗:

const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
  // do some calculations
}
Run Code Online (Sandbox Code Playgroud)

和这个 ?

for await (const res of items.map(e => somethingAsync(e))) {
  // do some calculations
}
Run Code Online (Sandbox Code Playgroud)

我知道在第一个片段中,所有的承诺都被同时触发,但我不确定第二个。for 循环是否等待第一次迭代完成以调用下一个 promise ?还是所有的 Promise 都是同时触发的,循环内部就像是它们的回调?

javascript node.js promise async-await for-await

24
推荐指数
3
解决办法
2万
查看次数

标签 统计

javascript ×2

arrays ×1

async-await ×1

for-await ×1

node.js ×1

promise ×1

split ×1