Skip to content

Instantly share code, notes, and snippets.

@sohamdodia
Last active May 31, 2018 09:44
Show Gist options
  • Save sohamdodia/f495e81ce622d6694f7d3e4cae2b5a2b to your computer and use it in GitHub Desktop.
Save sohamdodia/f495e81ce622d6694f7d3e4cae2b5a2b to your computer and use it in GitHub Desktop.
Async await Example.
/**
* without using async/await
*/
exports.updateUserAndSendEmail = (req, res) => {
User.findOne({ email: req.body.email }, (error, retrievedUser) => {
if (error) {
console.log('error');
}
if(retrievedUser) {
User.update({ email: email }, { name: 'XYZ' }, (error, result) => {
if (error) {
console.log('error', error);
}
EmailService.send(req.body.email, (error, result) => {
console.log('error', error);
console.log('result', result);
});
});
}
});
};
/**
* With using async/await
*/
exports.updateUserAndSendEmail = async (req, res) => {
try {
const retrievedUser = await User.findOne({ email: req.body.email });
if (retrievedUser) {
const updatedUser = await User.update({ email: req.body.email }, { name: 'XYZ' });
const emailResult = await EmailService.send(req.body.email);
}
} catch (error) {
console.log('error', error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment