Created
January 23, 2018 00:51
-
-
Save lubien/be87e883c1a02289fd714855972df141 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') | |
: 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