Skip to content

Instantly share code, notes, and snippets.

@bouchtaoui-dev
Last active January 15, 2019 14:01
Show Gist options
  • Select an option

  • Save bouchtaoui-dev/214d3794ca9ea2a3ef47ef52d608645d to your computer and use it in GitHub Desktop.

Select an option

Save bouchtaoui-dev/214d3794ca9ea2a3ef47ef52d608645d to your computer and use it in GitHub Desktop.
/**
* promise object
*/
function getUserWithId(id) {
return new Promise((resolve, reject) => {
// Look for a user with id in the database
User.find({_id: id}, (user, err) => {
if(err) return reject(err);
// on success
resolve(user); // from pending --> resolved
});
});
}
/**
*
*/
getUserWithId(someId)
.then(user => {
// we got a successfull object, send it back to the user or
// something like that.
})
.catch(ex => {
// send or throw an error
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment