I am learning to make a web app; I'm building a toy project on firebase.
Say I have some code like this:
db.doc(`/users/${username}`).get()
.then(doc => {
if (doc.exists) {
return res.status(400).json({message: "This username is already taken"});
}
return firebase.auth().createUserWithEmailAndPassword(username, password);
})
.then(data => {
// Do additional processing here
})
.then(data => {
return res.status(201).json({message: "New user created!"});
})
.catch(err => {
res.status(500).json({error: err});
});
It seems to me that in the first then
, if I returned res
, then the rest of the promise chain isn't executed. If I returned anything else, however, then the next function in the promise chain is passed the returned data and the chain keeps executing.
How does javascript (or maybe firebase?) know to distinguish between:
- when I return
res
, bail from the rest of the promise chain - when I return anything but
res
, continue the promise chain's execution?