Skip to content

Instantly share code, notes, and snippets.

@lubien
Created January 23, 2018 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lubien/be87e883c1a02289fd714855972df141 to your computer and use it in GitHub Desktop.
Save lubien/be87e883c1a02289fd714855972df141 to your computer and use it in GitHub Desktop.
const databaseGetUser = id =>
id === 'not-exists'
? Promise.reject('some weird database error')
: Promise.resolve({id, username: 'username'})
const databaseGetPosts = user =>
user.id === 'fail-to-get-posts'
? Promise.reject('some weird database error')
: Promise.resolve([{title: 'A'}, {title: 'B'}])
function getUserWithPosts(id) {
return databaseGetUser(id)
.then(user =>
databaseGetPosts(user)
.then(posts => {
return {user, posts}
})
.catch(err => { throw 'failed to load posts' }) // you expect this to work right?
)
.catch(err => { throw `user ${id} doesn't exists ` })
}
getUserWithPosts('lubien').then(console.log) // works fine
getUserWithPosts('not-exists').catch(console.error) // 'user not-exists doesn't exists'
getUserWithPosts('fail-to-get-posts').catch(console.error) // 'user fail-to-get-posts doesn't exists'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment