So, I'm super new to asynchronous and I cant understand how it works 'cause I can understand it but when I execute it, it seems that I didn't.
So, I have a code and I'm trying to run promises consecutively and carrying the variables and if it encounters an error it will go to catch.
Here's my simple code that I'm trying:
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;
I'm creating a promise which will
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.
that is what I'm trying to imagine the logic, code, and execution.
I'm using mysql, btw. For someone who can help me, please, I need you. Thanksss
If you're curious of the real code I'm converting, this is what I made so far: https://pastebin.com/Ui5vcUDg
I'm using some just promise there and I'm thinking that if I use promise chaining, I can shorten or clean up my code.