Ben Aston
Ben Aston

Reputation: 55729

Promise chaining using then

Does function c get invoked before the promise returned from b is resolved?

function a() {
 var d = $q.defer();
 setTimeout(function() { d.resolve(); }, 10000);
 return d.promise;
}

function b() {
 var d = $q.defer();
 setTimeout(function() { d.resolve(); }, 10000);
 return d.promise;
}

function c() {
 var d = $q.defer();
 setTimeout(function() { d.resolve(); }, 10000);
 return d.promise;
}

a().then(b).then(c);

Upvotes: 1

Views: 157

Answers (2)

Madhu Magar
Madhu Magar

Reputation: 445

Simply No. The promise b must be first resolved to execute c.

Upvotes: 2

hon2a
hon2a

Reputation: 7214

It doesn't. If a function provided to Promise.then (which returns promise A) returns a Promise (promise B), A waits on B to be resolved before resolving itself. (See documentation.)

Upvotes: 0

Related Questions