The code below is causing a race condition when it is checked with ESLint, reporting a violation of the require-atomic-updates rule:
let match = false
try {
match = await something()
} catch (err) {
// do something
}
if (match === false) {
// do something
}
What is the better way of writing this block of code?
Here's my actual code:
let request = ctx.request.body || {}
let password = request.password
let match = false
try {
match = await bcrypt.compare(password, 'xxxxx')
} catch (err) {
ctx.throw(401, err)
}
if (match === false) {
ctx.throw(401, 'invalid password')
}
ctx.body = {
message: 'logged in ok'
}
Error from ESLint:
Possible race condition:
ctx.body
might be reassigned based on an outdated value ofctx.body
require-atomic-updates
I realise it is a little bit late for this answer but just for any future users coming across this question, to disable this rule, in your .eslintrc.json
or whichever relevant config you use, just specify:
"require-atomic-updates": "off"