node.js中的asynchronous任务
我有四个同步工作的任务。
它看起来像这样:
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,我需要asynchronous运行。 第四个任务,即保存到数据库不应因此被阻塞。
现在这个任务是我要asynchronous做的,还有三个更多的任务:1.从db中获取设置2.作出curl请求3.在数据库中保存
这三个任务我可以使用async.waterfall
或者我希望会有任何替代品?
我如何在上面提到的函数createEntity
执行这个任务?
如果你不想等待asynchronous任务,只需调用它,不要等待它。 这很容易。
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) }); }