Created
January 23, 2018 00:57
-
-
Save lubien/7be9a38a3f1e5b028ef026014f6faad2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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