当数组中的某个函数没有返回promise时,我可以使用$q.all吗

Can I use $q.all when one of the functions in the array does not return a promise?

本文关键字:可以使 我可以 all promise 返回 数组 函数      更新时间:2023-09-26

我有一个函数,我想返回一个promise:

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以返回promise?

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

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

将打印1, 2, 3