Flattening Nested Promises in Javascript

Justin Tulk
May 15, 2017 · 1 min read
// login a user, and then retrieve a JWT token in Firebase
firebase.auth()
.signInWithEmailAndPassword('j@j.com', '12341234')
.then(user => {
user.getToken().then(token => {
// store the token in the Redux store
this.props.setUserJWTToken(token)
})
})

.catch(err => {
console.error(err)
});
// the error will bubble up so a single catch at the bottom handles
// both async calls
// login a user, and then retrieve a JWT token in Firebase
firebase.auth()
.signInWithEmailAndPassword('j@j.com', '12341234')
.then(user => {
return user.getToken()
})
.then(token => {
this.props.setUserJWTToken(token)
})

.catch(err => {
console.error(err)
});

Justin Tulk

Written by

Senior Engineer @ The Baby Box Co. Making computers do stuff since 2011.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade