After reorganizing this sequence, I am perplexed at how it continues to function regardless of a conditional return statement in one of the .then
sections:
function addList(name) {
let listObj = {};
listObj.name = name;
return nameExists(name) //returns a promise<boolean>
.then((result) => {
console.log("foo");
return result; //even without this return statement, the chain would not work as expected
//since it does not deliver a
//promise otherwise.
})
.then(bool => {
listObj.unique = !bool;
if (validListID(name)) { //this is a synchronous regex function
listObj.unique = false;
}
if (!listObj.unique)
return Counters.getNewShortListId(); // returns Promise
//The next chain still gets executed even when the condition isn't met,
//which is intriguing given that no promise is returned.
})
.then((id) => { //even if listObj.unique = true, this section runs smoothly,
//and it functions flawlessly, but why?
listObj.__id = id;
return new List(listObj).save();
});
}
The behavior of this code has left me puzzled. Isn't the promise chain supposed to break without a return promise?