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

https://github.com/restify/node-restify/blob/2053ef6a7e16d380a4e33d40059ea987c7373e4c/lib/chain.js#L77-L101

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)

Most upvoted comments

You can workaround this by wrapping all async handlers, like the package https://www.npmjs.com/package/@gilbertco/restify-async-wrap does.

export const wrap = (fn: any) => (req: Request, res: Response, next: Next) => {
  fn(req, res, next).catch(next);
};

then

app.post("/v1/users",wrap(someAsyncHandler))

Calling next is not allowed when using async functions. That said, I agree Restify must provide a way to stop chain processing, similarly to next(false);.

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.

In an async handler you have to throw an error instead of calling next(false) (@ghermeto, @gmahomarf). And for response.redirect(...) see #1916.

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.