Actually, I don't know how to check this on real Promise super class, so I'm testing it with a class extending it. But it seems to me the result would be the same.
class PromisePlus extends Promise {
constructor (handler) {
super(handler)
console.log('new promise created')
}
}
new PromisePlus((resolve, reject) => {
resolve()
}).then(() => {
return 'GOOD'
}).then(msg => {
console.log(msg)
})
prints 'new promise created' 3 times
new PromisePlus((resolve, reject) => {
resolve()
}).then(() => {
return PromisePlus.resolve('BAD')
}).then(msg => {
console.log(msg)
})
prints 'new promise created' 5 times.
The same result with returning new Promise inside then
handler
new PromisePlus((resolve, reject) => {
resolve()
}).then(() => {
return new PromisePlus((resolve, reject) => {
resolve('BAD')
})
}).then(msg => {
console.log(msg)
})
Obviously, PromisePlus.resolve()
creates new promise, but why does returning PromisePlus.resolve()
inside then
cause one more extra promise creation?