返回对调用者的响应并继续其他事情

时间:2014-02-06 12:52:15

标签: javascript node.js

我有api方法看起来像这样。 我的问题是,这是否可能以某种方式。没有它,实际的呼叫者必须这样做 等待,直至 someotherFunction();完了。

exports.callback = function (req, res) {  
  getValue()
   .then(function (result) {
      return res.send(200);
    })
   .catch(function () {
      return res.send(404);
   });

   //do other stuff here without holding up the response
   someotherFunction();

 }; 

1 个答案:

答案 0 :(得分:0)

返回不会逃避exports.callback,因为它们在另一个函数中...(匿名函数)

这样:

getValue().then(function (result) {
  return res.send(200);
})

只是一个内部函数调用的方法 在没有功能的情况下看起来像是这样的

 getValue().then(res.send(200))

所以返回会将res.send(200)放到getValue()。then();

不再是

所以exports.callback只会在someotherFunction()之后返回他的调用;已完成