gpt4 book ai didi

javascript - 沿 Promise 链传递值

转载 作者:行者123 更新时间:2023-11-30 19:50:28 25 4
gpt4 key购买 nike

<分区>

所以,我对异步非常陌生,我无法理解它是如何工作的,因为我可以理解它,但是当我执行它时,我似乎没有。

所以,我有一个代码,我正在尝试连续运行 promise 并携带变量,如果它遇到错误,它会去捕获。

这是我正在尝试的简单代码:

var x = new Promise(function(resolve, reject) {
var one = 1
resolve(one)
// if error happens go to catch
})
.then(function(value) {
var two = 2;
resolve(two);
// if error happens go to catch
})
.then(function(value) {
var three = 3;
resolve(three);
// if error happens go to catch
})
.then(function(value) {
console.log(one + two + three);
})
.catch(function(value) {
// reverse all the execution
})

x;

我正在创造一个 promise

1st promise with for loops to contain some object to be inserted

2nd promise that will insert some rows to my 1st table

3rd promise that will insert some rows from 1st table and some results from the second promise

4th promise will finish up somethings, etc

and a catch that will delete data that is inserted and cut up by the error.

这就是我试图想象的逻辑、代码和执行。

顺便说一句,我正在使用 mysql。对于可以帮助我的人,拜托,我需要你。谢谢

如果您对我正在转换的真实代码感到好奇,这是我目前所做的:https://pastebin.com/Ui5vcUDg

我在那里使用一些 just promise,我想如果我使用 promise 链,我可以缩短或清理我的代码。

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