Skip to content

Instantly share code, notes, and snippets.

View deepal's full-sized avatar

Deepal Jayasekara deepal

  • CompareTheMarket.com
  • London, United Kingdom
View GitHub Profile
// 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();
function myAsyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('oops'))
}, 1000);
});
}
(async() => {
try {
function myAsyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('oops'))
}, 1000);
})
}
myAsyncFunction()
.then(() => {
function myAsyncFunction(callback) {
setTimeout(() => {
callback(new Error('oops'))
}, 1000);
}
myAsyncFunction((err) => {
if (err) {
// handle error
} else {
#!/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=""
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');
}
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);
});
router.get('/users', async (req, res) => {
try {
res.status(200).send(await getUsers());
} catch (err) {
res.status(500).send({error: err.message});
}
})
async function getUser(userId) {
if (!userId) throw new InvalidInputError('userId is not provided');
try {
return getUserFromApi(userId);
} catch (err) {
throw err;
}
}
process.on('uncaughtException', (err) => {
logger.fatal('an uncaught exception detected', err);
});
process.on('unhandledRejection', (err) => {
logger.fatal('an unhandled rejection detected', err)
});