Here I have a promise that simply resolved.
let promise = new Promise((resolve,reject)=>{
resolve("resolved");
});
The confusion for me starts when I use a Promise for return value in .then
chain like this:
promise.then(resolve=>{
console.log(resolve);
return Promise.resolve(2);
}).then(resolve=>{
console.log(resolve);
});
promise.then(resolve=>{
console.log(resolve)
return 3;
}).then(resolve=>{
console.log(resolve);
});
The output of these chains is: 1 1 3 2 Which I expected to see: 1 1 2 3
But If I turn return Promise.resolve(2);
to just return 2
like here:
promise.then(resolve=>{
console.log(resolve);
return 2;
}).then(resolve=>{
console.log(resolve);
});
promise.then(resolve=>{
console.log(resolve)
return 3;
}).then(resolve=>{
console.log(resolve);
});
I'll get the output that I thought I would get in the first place (1 1 2 3).
So does anyone here can explain why the output changes based on using and not using Promise.resolve()
?
BTW I'm asking this question just for pure ACADEMIC reason!