简体   繁体   English

Javascript Promise 问题,为什么 Chain 不起作用

[英]Javascript Promise Question, why does Chain not work

So this works所以这有效

 new Promise(res => { console.log("1") res(res); }).then(res => { console.log("2") }).then(res => { console.log("3") })

But if I exclude the res(res), it will not chain.但是如果我排除 res(res),它就不会链接。 But it chains fine afterwards without any additional res().但它之后链接很好,没有任何额外的 res()。

Why is that, why does the first block matter if it has that res() and the subsequent blocks don't need it in order to chain onwards ?为什么会这样,为什么第一个块很重要,如果它有 res() 并且后续块不需要它以便向前链接?

Thanks.谢谢。

It chains just fine without the call to res() in that the succeeding .then() handlers are chained onto the original promise.它在没有调用res()情况下链接得很好,因为后续的.then()处理程序链接到原始承诺上。

But, the original promise will never be resolved so therefore, it will never call your .then() handlers.但是,原始承诺永远不会被解决,因此,它永远不会调用您的.then()处理程序。 At some point, you have to resolve the promise if you want the .then() handlers to get called.在某些时候,如果您希望.then()处理程序被调用,您必须解决承诺。

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

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