简体   繁体   English

在链中使用承诺和不承诺

[英]Using promises and non promises in a chain

I have a series of promises (mostly ajax calls but a few deffereds). 我有一系列的承诺(主要是ajax调用,但有一些延迟)。

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? 我这样称呼他们,我的问题是我还需要混入一些不返回promise的标准函数,下面有一段介绍,这是正确的方法吗?

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. 基本上,我希望诺言A,B和C运行,然后执行我的非诺言方法,一旦完成,就继续诺言D和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. then函数可以发回任何东西,包括未定义的东西。 This anything is encapsulated in a new Promise if needed so the chain can continue. 如有需要,这一切都封装在新的Promise中,以便链可以继续。

If your function returns a Promise or a thenable, the chain will continue when this object is fullfilled. 如果您的函数返回Promise或thenable,则当该对象已满时,链将继续。 promiseD will then receive the resolved value of this thenable. 然后promiseD将收到此thenable的已解析值。

If your function returns an immediate value or null or undefined, the chain will continue immediately. 如果您的函数返回立即数或为null或未定义,则链将立即继续。 promiseD will receive said value. promiseD将收到所述值。

So yes, your solution is correct. 是的,您的解决方案是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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