I am new to JavaScript and have encountered an interesting problem with the following code. Even though I haven't returned anything from the .then method, the last .then (blue color) works instead of the first one (red color). Can someone explain why this is happening?
let bodyBgChange = (color, delay) => {
return new Promise((resolve, reject) => {
setTimeout(()=>{
document.body.style.backgroundColor = color
resolve()
}, delay)
})
}
bodyBgChange('green',1000)
.then(()=>{
bodyBgChange('red', 1000)
})
.then(()=>{
bodyBgChange('yellow', 1000)
})
.then(()=>{
bodyBgChange('pink', 1000)
})
.then(()=>{
bodyBgChange('blue', 1000)
})