Skip to content

Commit

Permalink
chore: add lint rule to prevent floating promises (#404)
Browse files Browse the repository at this point in the history
* chore: add lint rule to prevent floating promises

* fix: add await or void calls to floating promises

* feat(FormModel): await pre-validate hook
  • Loading branch information
karrui committed Oct 5, 2020
1 parent df08618 commit 373761d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint", "import", "simple-import-sort"],
"extends": ["plugin:@typescript-eslint/recommended"],
Expand Down Expand Up @@ -50,7 +51,8 @@
"import/order": "off",
"import/first": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error"
"import/no-duplicates": "error",
"@typescript-eslint/no-floating-promises": 2,
}
},
{ "files": ["*.spec.ts"], "extends": ["plugin:jest/recommended"] }
Expand Down
4 changes: 2 additions & 2 deletions src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ const compileFormModel = (db: Mongoose): IFormModel => {
}

// Hooks
FormSchema.pre<IFormSchema>('validate', function (next) {
FormSchema.pre<IFormSchema>('validate', async function (next) {
// Reject save if form document is too large
if (bson.calculateObjectSize(this) > 10 * MB) {
const err = new Error('Form size exceeded.')
Expand All @@ -493,7 +493,7 @@ const compileFormModel = (db: Mongoose): IFormModel => {
}

// Validate that admin exists before form is created.
User.findById(this.admin, function (error, admin) {
await User.findById(this.admin, function (error, admin) {
if (error) {
return next(Error(`Error validating admin for form.`))
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/webhook/webhook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const post = (
if (webhookUrl) {
// Note that we push data to webhook endpoints on a best effort basis
// As such, we should not await on these post requests
pushData(webhookUrl, submissionWebhookView)
void pushData(webhookUrl, submissionWebhookView)
}
return next()
}
2 changes: 1 addition & 1 deletion src/loaders/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default async (): Promise<Connection> => {
emailDomain: 'data.gov.sg',
logo: '/public/modules/core/img/govtech.jpg',
})
agency.save()
await agency.save()
}

return mongoose.connection
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ const initServer = async () => {
})
}

initServer()
void initServer()

0 comments on commit 373761d

Please sign in to comment.