【发布时间】:2021-06-30 20:52:10
【问题描述】:
我将一个回调传递给 fs.writeStream,由于某种原因,它之后的任何任务都将首先被调用,然后再执行回调。
async function writeFile(file, callBack) {
// awaiting this also doesn't work
fs.writeFile(arg1, arg2, arg3, (error) => callBack(error))
}
async function task() {
let count = 0;
const callBack = (error) => {
if (error) {
count--;
} else {
count++;
}
}
for(const file of files) { // files.length = 1
await writeFile(file, callBack);
}
console.log(count) // Prints 0, should be 1. Why does this get called before the callback?
}
【问题讨论】:
标签: node.js asynchronous async-await fs