angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$q .always() Implementation

Open MandarinConLaBarba opened this issue 13 years ago • 11 comments

I know the goal is to keep $q lightweight, but any interest in an 'always' callback facility? Either an additional arg in .then()

.then(successCallback, errorCallback, alwaysCallback)

Or something like:

.then(callback).always(callback)

MandarinConLaBarba avatar Jan 17 '13 20:01 MandarinConLaBarba

+1

The upstream q package calls this .fin()

groner avatar Jan 24 '13 14:01 groner

Is this issue a duplicate of #1378, if so can we move discussion there and close this one maybe? :+1:

latentflip avatar Jan 29 '13 21:01 latentflip

They're different. The .fin() method is supposed to be like a finally block. The .end() method is a mechanism to say I'm done with this, if I missed something please treat it as an unhandled exception (log it, use $exceptionHandler, incite a riot).

groner avatar Jan 29 '13 22:01 groner

Oops, my bad. As you were :) On 29 Jan 2013 22:04, "Kai Groner" [email protected] wrote:

They're different. The .fin() method is supposed to be like a finally block. The .end() method is a mechanism to say I'm done with this, if I missed something please treat it as an unhandled exception (log it, use $exceptionHandler, incite a riot).

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1828#issuecomment-12860978.

latentflip avatar Jan 29 '13 22:01 latentflip

+1

maxbates avatar Apr 10 '13 08:04 maxbates

duplicate of #2424?

L42y avatar May 08 '13 03:05 L42y

Does this make sense with promise chaining? promise.then(callback).always(callback).then(callback again?)

MikeMcElroy avatar May 08 '13 13:05 MikeMcElroy

Yes. It is semantically equivalent to simply providing the same handler for both parameters of the then() function.

Pete ...from my mobile. On May 8, 2013 2:32 PM, "MikeMcElroy" [email protected] wrote:

Does this make sense with promise chaining? promise.then(callback).always(callback).then(callback again?)

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1828#issuecomment-17605575 .

petebacondarwin avatar May 08 '13 16:05 petebacondarwin

Provided that callback has no dependencies on what information it receives, or the resolved promise passed the same information as the rejected promise?

On Wed, May 8, 2013 at 9:16 AM, Pete Bacon Darwin [email protected]:

Yes. It is semantically equivalent to simply providing the same handler for both parameters of the then() function.

Pete ...from my mobile. On May 8, 2013 2:32 PM, "MikeMcElroy" [email protected] wrote:

Does this make sense with promise chaining? promise.then(callback).always(callback).then(callback again?)

— Reply to this email directly or view it on GitHub< https://github.com/angular/angular.js/issues/1828#issuecomment-17605575> .

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1828#issuecomment-17616548 .

MikeMcElroy avatar May 08 '13 16:05 MikeMcElroy

An always/finally callback receives no arguments and it resolves to same as the original promise unless it rejects.

    // Resolves to 5
    $q.when(5).always(function() { return 7 });

    // Resolves to 5 after 1 second
    $q.when(5).always(q.delay(1000) { return 7 });

    // Rejects with Error: nowayjose
    $q.when(5).always(function() { throw new Error('nowayjose') });

See https://github.com/kriskowal/q/wiki/API-Reference#promisefinallycallback.

groner avatar May 08 '13 17:05 groner

@groner - yes, sorry, you are absolutely right here.

By the way this is already in: https://github.com/angular/angular.js/pull/2424 - landed in master at https://github.com/angular/angular.js/commit/6605adf

petebacondarwin avatar May 08 '13 18:05 petebacondarwin