Home > front end >  Promise. Then (). Then () is executed in sequence? Why didn't the following code in order to pe
Promise. Then (). Then () is executed in sequence? Why didn't the following code in order to pe

Time:09-26

I always understand then (). Then () is a chain, each then returns a promise and then went back, but today, I ran a piece of code, not in this order execution,
In the following code, to run the third then, and then the first one, is the second again, may be linked to the setTime, but I don't know why,
Please directly copying my code, and then save as HTML, can run in the browser, notice that I output log

 




Promise. Resolve (' foo ')
//1. The Receive "foo" concatenate "bar" to it and resolve that the to the next then
Then (function (string) {
Return new Promise (function (resolve, reject) {
SetTimeout (function () {
String +='bar';
Resolve (string);
}, 1);
});
})
//2. The receive "foobar", register a callback function to work on the string
//and print it to the console, but not before the return of the unworked on
//the string to the next then
Then (function (string) {
SetTimeout (function () {
String +='baz';
The console. The log (string);
},
1)Return a string;
})
//3. Print helpful messages about how the code in this section will be run
//before the string is later processed by the mocked asynchronous code in the
//the prior then block.
Then (function (string) {
The console. The log (" Last Then: oops... Didn 't bother to instantiate the and return "+
"A promise in the prior then so the sequence may be a bit" +
"Surprising");

//Note that ` string ` will not have the 'baz' bit of it at this point, this
//is because we mocked that to happen asynchronously with a setTimeout function
The console. The log (string);
});

</script>

<body>




CodePudding user response:

I ran and played breakpoints, rectify the order now is 1-3 - & gt;" 2

CodePudding user response:

Execution order is just 2 log delay performed 123
 
Promise. Resolve (' foo ')
Then (function (string) {
The console. The log (1);
Return new Promise (function (resolve, reject) {
SetTimeout (function () {
String +='bar';
Resolve (string);
}, 1);
});
})
Then (function (string) {
The console. The log (2);
SetTimeout (function () {
String +='baz';
The console. The log (string);
},
1)Return a string;
})
Then (function (string) {
The console. The log (3);
The console. The log (" Last Then: oops... Didn 't bother to instantiate the and return "+
"A promise in the prior then so the sequence may be a bit" +
"Surprising");
The console. The log (string);
});

So watch out

CodePudding user response:

refer to the second floor zzgzzg00 response:
execution order is just 2 log delay performed 123

So you see

What you said is right, the second inside the log delay printed out,
More than 1) excuse me, then don't expression should be executed in sequence, I mean the first performed to execute the second, I think the second one should be the second delay the finished to perform the third, it's the chain ah, now I feel a few then is asynchronous, running at the same time, not like before a run to run behind,
2) and I also found that if within the first then settimeout hit on a log output, will find the first log and without delay, why then is different, and the second
 




Promise. Resolve (' foo ')
//1. The Receive "foo" concatenate "bar" to it and resolve that the to the next then
Then (function (string) {
The console. The log (1);
Return new Promise (function (resolve, reject) {
SetTimeout (function () {
The console. The log (11);
String +='bar';
Resolve (string);
}, 1);
});
})
//2. The receive "foobar", register a callback function to work on the string
//and print it to the console, but not before the return of the unworked on
//the string to the next then
Then (function (string) {
The console. The log (2);
SetTimeout (function () {
String +='baz';
The console. The log (string);
},
1)Return a string;
})
//3. Print helpful messages about how the code in this section will be run
//before the string is later processed by the mocked asynchronous code in the
//the prior then block.
Then (function (string) {
The console. The log (3);
The console. The log (" Last Then: oops... Didn 't bother to instantiate the and return "+
"A promise in the prior then so the sequence may be a bit" +
"Surprising");

//Note that ` string ` will not have the 'baz' bit of it at this point, this
//is because we mocked that to happen asynchronously with a setTimeout function
The console. The log (string);
});

</script>

<body>




CodePudding user response:

Because the first return for a promise then after the object is determined by the promise of the return and the second is not that you can see promise

CodePudding user response:

Zzgzzg00
reference 4 floor response:
because the first return for a promise then after the object is determined by the promise of the return and the second is not that you can see promise based

Then shouldn't () will return a Promise object? Why even designed his new a Promise

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related