The finally callback is called with no arguments.
It does, however, return a promise passing the results along.
Even fixing for that, you're not returning anything in your callbacks, so nothing is being passed along.
To illustrate this:
angular.module('myApp', [])
.run( function ($q) {
var defer = $q.defer();
defer.promise.then(
function ( res ) { console.log('s1> '+res); },
function ( res ) { console.log('e1> '+res); }
)
.then(
function ( res ) { console.log('s2> '+res); },
function ( res ) { console.log('e2> '+res); }
)
defer.reject(1);
});
Gives this:
e1> 1
s2> undefined
Notice that the second then
was "successful" because the reject didn't get passed on.
Make sure that your callbacks return something. And if you want to fall to the errback in subsequent then
s make sure that you return a rejection.
var defer = $q.defer();
defer.promise.then(
function ( res ) { console.log('s1> '+res); return res; },
function ( res ) { console.log('e1> '+res); return $q.reject(res); }
)
.then(
function ( res ) { console.log('s2> '+res); return res; },
function ( res ) { console.log('e2> '+res); return $q.reject(res); }
)
Putting that together gives something like this:
var defer = $q.defer();
defer.promise.then(
function ( res ) { console.log('s1> '+res); return res; },
function ( res ) { console.log('e1> '+res); return $q.reject(res); }
)
.then(
function ( res ) { console.log('s2> '+res); return res; },
function ( res ) { console.log('e2> '+res); return res; }
)
.finally (
function ( res ) {
console.log('f0> '+res+','+arguments.length);
}
)
.then(
function ( res ) { console.log('s3> '+res); return res; },
function ( res ) { console.log('e3> '+res); return $q.reject(res); }
)
defer.reject('foo');
Resulting in:
e1> foo
e2> foo
f0> undefined,0
s3> foo
Notice that the errback in the second then
returned res instead of a rejection, so the callback of the finally's then was called.
finally
callback isn't meant to take parameters because I believe in general,finally
clauses are guaranteed to be called after some execution to allow you to clean up resources and whatnot. – Nazarite