node-restify: New async/await handler support breaks `next(false)` functionality in current async handlers
- Used appropriate template for the issue type
- Searched both open and closed issues for duplicates of this issue
- Title adequately and concisely reflects the feature or the bug
Restify Version: 10.0.0 Node.js Version: 16.8.1
Expected behaviour
Given a handler that does async work, I should be able to call next(false);
and have the chain stop processing there.
Actual behaviour
The handler arity checks prevent me from having handlers that use next and are async
Repro case
Code similar to this is used in one of our projects using restify v8. It breaks when trying to update to v10:
server.use(async (req, res, next) => {
const result = await someAsyncWork();
if (shouldStop(result)) {
res.send({something: 'here'});
next(false);
return;
}
// ... more work
next();
});
I am aware I could make my handler synchronous, then do someAsyncWork().then(result => {...})
but async/await
syntax was chosen for cleanliness and readability.
Cause
Are you willing and able to fix this?
This probably requires reworking the async chain stuff, so no.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 7
- Comments: 16 (1 by maintainers)
You can workaround this by wrapping all async handlers, like the package https://www.npmjs.com/package/@gilbertco/restify-async-wrap does.
then
app.post("/v1/users",wrap(someAsyncHandler))
Yes, I’m well aware it’s not allowed. That’s the whole point of this issue, as is the inability to stop chain processing.
No, because I don’t want to throw an error. I want to stop the handler chain.
Agree. This is making me consider switching framework.