I recently restructured (flattened) this chain. However I am confused as to why this chain still works in any case even though the return
statement in one of the .then
links is conditional:
function addList(name) {
let listObj = {};
listObj.name = name;
return nameExists(name) //returns a promise<boolean>
.then((result) => {
console.log("foo");
return result; //without this return statement the chain would break,
//which makes sense to me because it does not return 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
//Does not return promise when condition not met.
//However the next chain gets called in any case.
})
.then((id) => { //this gets called even when listObj.unique = true,
//also this works perfectly, why?
listObj.__id = id;
return new List(listObj).save();
});
}
I am really confused as to why this behaves the way it does. I thought the promise chain breaks when no promise is returned?