AngularJS 为什么要对一个承诺使用 $q.all() ?
AngularJS Why use $q.all() on one promise?
我正在熟悉一个代码库,我到处都看到这样的代码:
$q.all([promise]).then(responseFunc);
这对我来说没有意义 -- 我已经阅读了文档,但我不知道为什么不使用以下内容,因为它已经是一个承诺...
promise.then(responseFunc);
有什么我想念的吗?前者比后者有什么优势?
是的,这有点奇怪,但有一点不同:responseFunc
将使用结果数组而不是结果本身来调用。
这可能最好写成
promise.then(res => responseFunc([res]))
或
promise.then(Array.of).then(responseFunc)
好的,这是我能想到的唯一优点(基于我上面的评论)
function responseFunc(arr) {
arr.forEach(data => {
// do stuff with data
});
}
$q.all([promise1, promise2]).then(responseFunc);
$q.all([promise]).then(responseFunc);
我正在熟悉一个代码库,我到处都看到这样的代码:
$q.all([promise]).then(responseFunc);
这对我来说没有意义 -- 我已经阅读了文档,但我不知道为什么不使用以下内容,因为它已经是一个承诺...
promise.then(responseFunc);
有什么我想念的吗?前者比后者有什么优势?
是的,这有点奇怪,但有一点不同:responseFunc
将使用结果数组而不是结果本身来调用。
这可能最好写成
promise.then(res => responseFunc([res]))
或
promise.then(Array.of).then(responseFunc)
好的,这是我能想到的唯一优点(基于我上面的评论)
function responseFunc(arr) {
arr.forEach(data => {
// do stuff with data
});
}
$q.all([promise1, promise2]).then(responseFunc);
$q.all([promise]).then(responseFunc);