// add 异步相加
function add(a, b) {
return new Promise((resolve, reject) => {
setTimeout(()...=> {
resolve(a + b);
}, 1000);
});
}
// 传入不定长度的数组,与 add 函数,对数组的每个值进行相加,串行相加
function..., 3, 4, 5], add).then((res) => {
console.log(res); // 15
})
);
// 使用 Promise.all 来实现读个任务并行执行...,tasks 为异步任务列表
const tasks = [];
for (const chunk of chunks) {
// 每一个 task 中的数组单独进行计算...sumPoll([1, 2, 3, 4, 5, 6, 7, 8, 9, , 10], add, 3).then((result) =>
console.log(result)
);
参考代码