如果在任何控制器中调用异步函数,那么以下两个路由之间有什么区别:

router.post('/proxy/notify', function (req, res) {
    res.send("ok");
    postOptions.uri = req.body.uri;
    delete req.body.uri;
    postOptions.form = req.body;
    return request(postOptions).then(console.log).catch(console.log);
});


现在,即使我跳过最后一行中的返回值,它也仍然有效:

router.post('/proxy/notify', function (req, res) {
    res.send("ok");
    postOptions.uri = req.body.uri;
    delete req.body.uri;
    postOptions.form = req.body;
    request(postOptions).then(console.log).catch(console.log);
});


我可以看到的唯一好处是return可以确保在request()函数调用之后(如果有的话)不执行任何行。

最佳答案

除非您使用承诺承诺中间件(假设它是express),否则您将使用否。目前没有意义。

也就是说,express将来可能会使用它。

关于javascript - 在NodeJS的 Controller 中返回 promise 有什么意义?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37497265/

10-16 14:22