Using promises and non promises in a chain

112 views Asked by At

I have a series of promises (mostly ajax calls but a few deffereds).

I call them like so, my question is I also need to mix in some standard functions that do not return promises, I have had a go below, is this the correct way to do it?

Basically i want promise A, B and C to run, then do my non promise methods, once these are done, carry on with promise D and E.

this.promiseA()
.then(this.promiseB)
.then(this.promiseC)
.then(function(){

    //do some methods that do not return promises
})
.then(this.promiseD)
.then(this.promiseE);
2

There are 2 answers

9
guest271314 On
this.promiseA()
.then(this.promiseB)
.then(this.promiseC)
.then(function(){

    //do some methods that do not return promises
    // modify original promise here , else original promise
    // passed to next `.then` in chain
    // return $.when(true); 
})
.then(this.promiseD)
.then(this.promiseE);
0
solendil On

then functions can send back anything, including undefined. This anything is encapsulated in a new Promise if needed so the chain can continue.

If your function returns a Promise or a thenable, the chain will continue when this object is fullfilled. promiseD will then receive the resolved value of this thenable.

If your function returns an immediate value or null or undefined, the chain will continue immediately. promiseD will receive said value.

So yes, your solution is correct.