Multiple chained deferred functions using q in AngularJS stop returning data Multiple chained deferred functions using q in AngularJS stop returning data angularjs angularjs

Multiple chained deferred functions using q in AngularJS stop returning data


Excerpt taken from the official doc on $q:

then(successCallback, errorCallback) – regardless of when the promise was or will be resolved or rejected calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument the result or rejection reason.

This method returns a new promise which is resolved or rejected via the return value of the successCallback or errorCallback.

And for the return value of the successCallack or errorCallback, according to Domenic's slides:

if the return value is a promise then the promise adopts the returned promise's state otherwise the success callback is immediately called with the return value

Based on the definition, your code is missing the return keyword. It should be as following:

    asyncFn1(1)    .then(function(data){return asyncFn2(data)})    .then(function(data){return asyncFn2(data)})    .then(function(data){return asyncFn2(data)});