gpt4 book ai didi

javascript - promise : How to pass data?

转载 作者:行者123 更新时间:2023-11-30 15:24:05 26 4
gpt4 key购买 nike

我想了解 promises 是如何运作的。我看到它就像一个“链式回调”……或多或少……但是……可以将您获得的值提取到外部值/数组/对象吗?

Promise1()
.then(return x)
.then()
.then()
.then(function(x){
console.log(x);
});

我如何将数据从第一个 .then() 传递到第四个 .then()

任何帮助我理解 promise 的努力都将不胜感激。

最佳答案

你可以做到,但你需要将 x 值从一个 then 传递给另一个:

Promise1()
.then(function() {
return x; // The "x" is wrapped in a resolved promise by "then method"
})
.then(function(x) { // The "then" pass to the fulfillment callback the value inside the promise taken in input, which is the "x" value
return x; //Same as above, we return the "x" value to pass it to the next "then" function
})
.then(function(x) { // Same as above
return x; // Same as above
})
.then(function (x) {
console.log(x); // Here you can access the "x" value and print it
})

关于javascript - promise : How to pass data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43252034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com