I have the below javascript code that I wrote to understand the async behavior
console.log(1);
let myPromise = () => {
//return Promise.resolve("success");
return new Promise((resolve, reject) => {
for (let i = 0; i < 10000000; i++) {
let p = i ** 2;
}
console.log('loop complete');
resolve('success');
})
}
console.log(2);
myPromise()
.then((m) => {
console.log(m);
return myPromise()
})
.then((m) => console.log(m))
.catch((e) => console.log(e))
console.log(3);
My expected output is
1
2
loop complete
success
loop complete
success
3
since .then()
chaining is synchronous (or it is not ). But the actual output is
1
2
loop complete
3
success
loop complete
success
I can't understand why the output is like that? Can anyone explain the behavior?