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
| // Example 1 | |
| try { | |
| return await myAsyncFunction(); | |
| } catch (err) { | |
| // Any promise rejection while calling myAsyncFunction() will reach here, because of using `await` | |
| } | |
| // Example 2 | |
| try { | |
| return myAsyncFunction(); |
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
| function myAsyncFunction() { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| reject(new Error('oops')) | |
| }, 1000); | |
| }); | |
| } | |
| (async() => { | |
| try { |
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
| function myAsyncFunction() { | |
| return new Promise((resolve, reject) => { | |
| setTimeout(() => { | |
| reject(new Error('oops')) | |
| }, 1000); | |
| }) | |
| } | |
| myAsyncFunction() | |
| .then(() => { |
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
| function myAsyncFunction(callback) { | |
| setTimeout(() => { | |
| callback(new Error('oops')) | |
| }, 1000); | |
| } | |
| myAsyncFunction((err) => { | |
| if (err) { | |
| // handle error | |
| } else { |
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
| #!/bin/bash | |
| GIT_HOST=$(git remote -v | grep fetch | cut -d "@" -f2 | cut -d ":" -f1) | |
| GIT_PROJECT=$(git remote -v | grep fetch | cut -d "@" -f2 | cut -d ":" -f2 | cut -d "." -f1) | |
| GIT_URL="https://$GIT_HOST/$GIT_PROJECT" | |
| TAG1=$1 | |
| TAG2=$2 | |
| if [ -z $2 ] | |
| then | |
| TAG2="" |
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
| schema.pre('findOneAndUpdate', function (next) { | |
| const MongooseValidationError = mongoose.Error.ValidationError, | |
| model = this.model(), | |
| immutableFields = ['email', 'password'], | |
| update = this.getUpdate(); | |
| immutableFields.forEach((field) => { | |
| if (update.hasOwnProperty(field)) { | |
| model.invalidate(field, `${field} cannot be modified`, update[field], 'immutable'); | |
| } |
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
| process.on('uncaughtException', (err) => { | |
| logger.fatal('an uncaught exception detected', err); | |
| process.exit(-1); | |
| }); | |
| process.on('unhandledRejection', (err) => { | |
| logger.fatal('an unhandled rejection detected', err); | |
| process.exit(-1); | |
| }); |
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
| router.get('/users', async (req, res) => { | |
| try { | |
| res.status(200).send(await getUsers()); | |
| } catch (err) { | |
| res.status(500).send({error: err.message}); | |
| } | |
| }) |
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
| async function getUser(userId) { | |
| if (!userId) throw new InvalidInputError('userId is not provided'); | |
| try { | |
| return getUserFromApi(userId); | |
| } catch (err) { | |
| throw err; | |
| } | |
| } |
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
| process.on('uncaughtException', (err) => { | |
| logger.fatal('an uncaught exception detected', err); | |
| }); | |
| process.on('unhandledRejection', (err) => { | |
| logger.fatal('an unhandled rejection detected', err) | |
| }); |