Flattening Nested Promises in Javascript

// 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)
});

--

--

Staff Software Engineer. Making computers do stuff since 2011.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Justin Tulk

Justin Tulk

Staff Software Engineer. Making computers do stuff since 2011.