Created
November 16, 2016 17:31
-
-
Save definitelycarter/1bce70c3c30a23a431b50acfecaec752 to your computer and use it in GitHub Desktop.
Example using promises.
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
let getUserFromEmail = (email, cb) => { | |
$service.findUser({ email }, (err, user) => { | |
if(err) { | |
return cb(err); | |
} else if(user == null) { | |
return cb(new Error('The user was not found')); | |
} | |
console.log('User found'); | |
return cb(undefined, user); | |
}); | |
} | |
let getUserFromEmailAsync = (email) => { | |
return Promise(function(resolve, reject) { | |
getUserFromEmail(email, (e, user) => { | |
if (e) { | |
reject(e); | |
} else { | |
resolve(user); | |
} | |
}); | |
}); | |
} | |
console.log('Fetching user'); | |
getUserFromEmailAsync('[email protected]') | |
.then((user) => { | |
console.log(user.email) // [email protected] | |
}) | |
.catch((reason) => { | |
console.log('Error occurred'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment