简体   繁体   English

AngularJS为什么在一个承诺上使用$ q.all()?

[英]AngularJS Why use $q.all() on one promise?

I'm familiarizing myself with a codebase, and I'm seeing this everywhere: 我熟悉一个代码库,我到处都看到了这个:

$q.all([promise]).then(responseFunc);

This does not make sense to me -- I've read the documentation, and I don't know why the following is not used instead, since it's already one promise... 这对我来说没有意义 - 我已经阅读了文档,我不知道为什么不使用以下内容,因为它已经是一个承诺......

promise.then(responseFunc);

Is there something I'm missing? 有什么我想念的吗? What's the advantage of the former over the latter? 前者优于后者有什么优势?

Yes, this is a bit weird, but there is a difference: responseFunc will be called with an array of the result instead of the result itself. 是的,这有点奇怪,但有一点不同:将使用结果数组而不是结果本身调用responseFunc

This probably should better be written as either 这可能最好写成两者

promise.then(res => responseFunc([res]))

or 要么

promise.then(Array.of).then(responseFunc)

Ok, here's the only advantage I can think of (based on my comment above) 好的,这是我能想到的唯一优势(基于我上面的评论)

function responseFunc(arr) {
    arr.forEach(data => {
        // do stuff with data
    });
}

$q.all([promise1, promise2]).then(responseFunc);
$q.all([promise]).then(responseFunc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM