Wulf Wulf - 6 months ago 7
Javascript Question

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?

Answer

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.

This probably should better be written as either

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

or

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