回调不是异步库中的函数
callback is not a function inside async library
我创建了这个小代码来重现我在应用程序中遇到的错误:
const async = require("async");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const parallels = [];
for (let i = 0; i < 2; i++) {
parallels.push(async function (callback) {
const randomSleepTime = Math.ceil(Math.random() * 10000);
await sleep(randomSleepTime);
callback(randomSleepTime);
});
}
async.parallel(parallels, function (err, results) {
console.log(err);
console.log(results);
});
完成后出现此错误:TypeError: callback is not a function
为什么会这样?
解决方案是 return 值而不是使用 here.
中提到的 callback()
我创建了这个小代码来重现我在应用程序中遇到的错误:
const async = require("async");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const parallels = [];
for (let i = 0; i < 2; i++) {
parallels.push(async function (callback) {
const randomSleepTime = Math.ceil(Math.random() * 10000);
await sleep(randomSleepTime);
callback(randomSleepTime);
});
}
async.parallel(parallels, function (err, results) {
console.log(err);
console.log(results);
});
完成后出现此错误:TypeError: callback is not a function
为什么会这样?
解决方案是 return 值而不是使用 here.
中提到的callback()