我有四个同步工作的任务。
看起来像这样:
function createEntity(req, res, next) {
validator.validateInternalProductRequest(req.body)
.then((data) => feedbackSetting.getUrl(data))
.then((data) => bitlyService.shortenLink(data))
.then((data) => internalProductService.saveInternalProductRequest(data))
.then((data) => res.send(data))
.catch(err => {
console.log(err);
next(err)
});
}
现在在第三个和第四个任务之间,即在获得短链接之后和保存到数据库之前,我需要执行一个任务,假设任务 AS 我需要异步运行。第四个任务,即保存到数据库不应该因此而被阻止。
现在我必须异步执行的这个任务还有另外三个任务: 1.从数据库获取设置 2. 发出curl请求 3.保存到数据库
我可以使用async.waterfall
来完成这三个任务,或者我希望有其他替代方案?
如何在上述函数 createEntity
中执行此任务 AS?
最佳答案
如果您不想等待异步任务,只需调用它即可,不要等待它。就这么简单。
function createEntity(req, res, next) {
validator.validateInternalProductRequest(req.body)
.then((data) => feedbackSetting.getUrl(data))
.then((data) => {
callSomeAsyncMethodAndDontWaitForResult();
return bitlyService.shortenLink(data)
}).then((data) => internalProductService.saveInternalProductRequest(data))
.then((data) => res.send(data))
.catch(err => {
console.log(err);
next(err)
});
}
关于node.js - Node.js 中的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37966191/