【发布时间】:2021-03-30 16:29:20
【问题描述】:
我有一个使用 3rd 方包的服务。
exports.FOO = (options, cb) => {
const outputPath = 'bar'
const storePDF = async (data) => {}
const sendBack = (outputPath, cb) => {
fs.readFile(outputPath, encoding, async (err, data) => {
if (err) {
return cb(err)
}
if (options.source) {
await store(data)
}
cb(null, data);
})
}
ThirdParty.run(err => {
if (err) {
return cb(err)
};
// read the file and send it back
sendBack(outputPath, cb)
})
}
我想升级这个包,但它有一些重大变化,新的用法如下:
await ThirdParty.setup()
await ThirdParty.run()
我真的很想保留回调,因为这个服务在整个应用程序中都是这样使用的。我试图让 FOO async 并在 await ThirdParty.run() 之后调用 sendBack,但我得到了。
node_modules/async/dist/async.js:966
if (fn === null) throw new Error("Callback is already called.");
错误:已调用回调。
exports.FOO = async (options, cb) => {
const outputPath = 'bar'
const storePDF = async (data) => {}
const sendBack = (outputPath, cb) => {
fs.readFile(outputPath, encoding, async (err, data) => {
if (err) {
return cb(err)
}
if (options.source) {
await store(data)
}
cb(null, data);
})
}
await ThirdParty.setup()
await ThirdParty.run()
sendBack(outputPath, cb)
}
【问题讨论】:
标签: node.js async-await