【问题标题】:Can I use $q.all when one of the functions in the array does not return a promise?当数组中的一个函数没有返回承诺时,我可以使用 $q.all 吗?
【发布时间】:2016-04-30 02:34:28
【问题描述】:

我有这个函数,我想返回一个承诺:

homeWordsResolve = (): ng.IPromise<any> => {
    var self = this;
    return this.$q.all([
        self.ens.getUserProfile(),
        self.wos.getWordsOrderBy(),
        self.wos.getPos()
    ]);
}

但是其中一个函数是同步的,只返回一个 void。

getWordsOrderBy = (): void => {
    this.wordsOrderBy = this.ens.getOrderBy(EnumGetOrderBy.Word)
}

有什么方法可以让我继续使用 $q.all 并修改 getWordsOrderBy 以返回一个承诺?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    是的,你可以,$q.all 接受一组承诺或值。

    $q.all([
      $q.resolve(1),
      $q.resolve(2),
      3
    ])
    .then(function(results) {
      console.log(results);
    });
    

    将打印1, 2, 3

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多