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