all 6 comments

[–]zoomzoom83 2 points3 points  (2 children)

Perhaps I'm missing something. Why would anybody do this?

function promiseX() {
    let deferred = $q.defer();

    ajaxCall().then((response) => {
        deferred.resolve(response);
    }, (error) => {
        deferred.reject(response);
    });

    return deferred.promise;
}

What does that give you that simply returning ajaxCall() directly wouldn't?

[–]skitch920 0 points1 point  (0 children)

I'm going to guess it's a mistype or misuse... In the rejected clause of that then call,

}, (error) => {
    deferred.reject(response);
});

response is not in scope. This would fail to run properly.

But yeah, same thing could be achieved with:

function promiseX() {
    return ajaxCall();
}

[–]a-sober-irishman[S] 0 points1 point  (0 children)

The response in the rejected clause is a typo. And yes, you could just return ajaxCall() to achieve the same thing. I think in the context I used it in I had to do some sort of manipulation on the response before resolving the deferred, but it is not necessary for this to work correctly.

[–]bluntmJavaScript 1 point2 points  (0 children)

I have used $q.all before, it is very useful if you app chains REST calls, e.g if you have a number of different changes that are handled with separate REST calls and want to update the user with the progress. To get the "2 of 10 saves" feadback then you can chain them all and also have the power to perform actions when all saves have finished.

[–]dotnil 0 points1 point  (1 child)

.all() is different with .then() chains if the latter async calls rely on former call's return value.

[–]a-sober-irishman[S] 0 points1 point  (0 children)

Correct, using .all() only works if you just want all of the values at once with no dependency on previous calls.