Removing nested promises(移除嵌套的 Promise)
问题描述
我不熟悉 Promise 和使用 NodeJS 中的请求和 Promise 编写网络代码.
I'm new to promises and writing network code using requests and promises in NodeJS.
我想删除这些嵌套的 Promise 并将它们链接起来,但我不确定我将如何去做/这是否是正确的方法.
I would like to remove these nested promises and chain them instead, but I'm not sure how I'd go about it/whether it is the right way to go.
这是请求代码:
任何见解将不胜感激.
推荐答案
从每个 then
回调中,您需要返回新的承诺:
From every then
callback, you will need to return the new promise:
.then()
调用返回的 Promise 将使用内部"promise 中的值解析,因此您可以轻松地将它们链接起来.
The promise that is returned by the .then()
call will then resolve with the value from the "inner" promise, so that you easily can chain them.
通用模式:
这篇关于移除嵌套的 Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!