简体   繁体   中英

Using promises and non promises in a chain

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);
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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM