【问题标题】:Returning a promise to the caller向调用者返回一个承诺
【发布时间】:2013-04-17 14:28:03
【问题描述】:

我有一个函数HelloWorld,其中没有向调用者返回任何承诺。我想知道是否可以调整此功能以返回承诺。

例如:

function HelloWorld() {
    // Do something here
    return;
}

在我的应用程序的另一个功能中,我想做:

...
return HelloWorld()
        .then( ... do something else here ... );

如何继续让这个函数返回一个承诺?

【问题讨论】:

  • 您确定需要承诺吗?仅在使用异步进程时才需要延迟/承诺。

标签: javascript jquery promise


【解决方案1】:
function HelloWorld() {
  var deferred = $.Deferred();

  // demo
  setTimeout(deferred.resolve, 1000);

  // this is what you return:
  return deferred.promise();
}

【讨论】:

    【解决方案2】:

    HelloWorld

       var dfr = $.Deferred();
      //some work with it
      return dfr.promise();
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 2016-06-15
      • 2015-06-05
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多