说我有一个要迭代的数字数组。在此数组中,我不关心表达式的完成顺序,仅关心表达式的表达。 (就数字而言,数字4将比所有其他数字花费更长的时间来完成,但我不希望这会干扰其他数字的迭代)
要澄清
const arr = [1,2,3,4,5,6,7,8,9,10];
即:
for (const num of arr) {...}
vs:
/*The for loop itself isnt async but the iteration would be*/
async for (const num of arr) {/*The code that would be done asynchronously*/}
我想这会类似于吗?
const stringTogether = (n) => n + ', '
const loop = async(n, max, cb, val) => {
if (n === max) return val;
else return loop(n + 1, max, cb, cb(val));
}
const string = loop(0, arr.length, stringTogether);
或
for (const num of arr) {
setTimeout(() => arr2.push(num * num), 1000);
}