now use a code like this:
(() => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("task1 -> 3s")
resolve()
}, 3000);
})
.then(() => {
setTimeout(() => {
console.log("task2 -> 2s")
Promise.resolve()
}, 2000);
})
.then(() => {
setTimeout(() => {
console.log("task3 -> 1s")
}, 1000);
})
})()
task1 -> task3 -> task2
if you change it to:
(() => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("task1 -> 3s")
resolve()
}, 3000);
})
.then(() => {
return new Promise((resolve) => {
setTimeout(() => {
console.log("task2 -> 2s")
resolve()
}, 2000);
})
})
.then(() => {
setTimeout(() => {
console.log("task3 -> 1s")
}, 1000);
})
})()
is fine
, but if you change the second then to return another promise, it will be no problem. I would like to ask why, my idea is, the first way to use Promise.then () is not to wait for this asynchronous function to finish execution before executing the following?
Please tell me what is wrong. Thank you ~