Skip to content

Instantly share code, notes, and snippets.

@lubien
Created January 23, 2018 00:57
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/7be9a38a3f1e5b028ef026014f6faad2 to your computer and use it in GitHub Desktop.
Save lubien/7be9a38a3f1e5b028ef026014f6faad2 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 for posts')
: Promise.resolve([{title: 'A'}, {title: 'B'}])
async function getUserWithPosts(id) {
const user = await databaseGetUser(id).catch(err => { throw `user ${id} doesn't exists ` })
const posts = await databaseGetPosts(user).catch(err => { throw 'failed to load posts' })
return {user, posts}
}
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) // 'failed to load posts'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment