oxc/no-async-endpoint-handlers
oxc plugin · suspicious
No autofix
.oxlintrc.json
{
"rules": {
"oxc/no-async-endpoint-handlers": "error"
}
}Disallows the use of async functions as Express endpoint handlers.
const app = express()
app.get('/', (req, res, next) => {
new Promise((resolve, reject) => {
return User.findById(req.params.id)
})
.then(user => res.json(user))
.catch(next)
})const app = express()
app.get('/', async (req, res) => {
// Server will crash if User.findById rejects
const user = await User.findById(req.params.id)
res.json(user)
})const app = express();
app.get('/', async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
});
const router = express.Router();
router.use(async (req, res, next) => {
const user = await User.findById(req.params.id);
req.user = user;
next();
});
const createUser = async (req, res) => {
const user = await User.create(req.body);
res.json(user);
}
app.post('/user', createUser);
// Async handlers that are imported will not be detected because each
// file is checked in isolation. This does not trigger the rule, but still
// violates it and _will_ result in server crashes.
const asyncHandler = require('./asyncHandler');
app.get('/async', asyncHandler);const app = express();
// not async
app.use((req, res, next) => {
req.receivedAt = Date.now();
})
app.get('/', (req, res, next) => {
fs.readFile('/file-does-not-exist', (err, data) => {
if (err) {
next(err) // Pass errors to Express.
} else {
res.send(data)
}
})
})
const asyncHandler = async (req, res) => {
const user = await User.findById(req.params.id);
res.json(user);
}
app.get('/user', (req, res, next) => asyncHandler(req, res).catch(next))Examples from the Oxlint documentation.
ESLint does not have this rule.
oxc/no-async-endpoint-handlers
oxc plugin · suspicious
{
"rules": {
"oxc/no-async-endpoint-handlers": "error"
}
}Biome does not have this rule.
Prevents using object or array spreads on accumulators in Array.prototype.reduce() and in loops.
Disallow the use of barrel files where the file contains export * statements, and the total number of modules exceeds a threshold.
Https://rust-lang.github.io/rust-clippy/master/#/misrefactored_assign_op
Disallows the use of approximate constants, instead preferring the use of the constants in the Math object.
Disallows the use of this in exported functions.