Should I return the result of deferred.resolve/reject?

我是研究僧i 提交于 2019-12-13 06:02:13

问题


When working with Q deferreds, should I return the result of deferred.resolve and deferred.reject?

function foo() {
  var deferred = Q.defer();
  service.doSomethingAsync({
    success: function() {
      deferred.resolve(); // should I return the result of resolve here?
    }, 
    fail: function(err) {
      deferred.reject(err); // should I return the result of reject here?
    }
  });

  return deferred.promise;
}

回答1:


Your code can be changed to:

function foo() {
  var deferred = Q.defer();

  service.doSomethingAsync({
    success: deferred.resolve, 
    fail:  deferred.reject
  });

  return deferred.promise;
}

What you want to return from the foo() method depends on what you want to achieve of course. In many cases you hide the internals and just return an empty array or null if something fails. But..if it is needed...you can throw an error. If you want to handle things outside the function, yes, return the error object for example...like I said...it depends.



来源:https://stackoverflow.com/questions/32053334/should-i-return-the-result-of-deferred-resolve-reject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!