Last active
May 31, 2018 09:44
-
-
Save sohamdodia/f495e81ce622d6694f7d3e4cae2b5a2b to your computer and use it in GitHub Desktop.
Async await Example.
This file contains hidden or 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
/** | |
* 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