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
|
if (handler.length <= 2) { |
|
// arity <= 2, must be AsyncFunction |
|
assert.equal( |
|
handler.constructor.name, |
|
'AsyncFunction', |
|
`Handler [${handlerId}] is missing a third argument (the ` + |
|
'"next" callback) but is not an async function. Middleware ' + |
|
'handlers can be either async/await or callback-based.' + |
|
'Callback-based (non-async) handlers should accept three ' + |
|
'arguments: (req, res, next). Async handler functions should ' + |
|
'accept maximum of 2 arguments: (req, res).' |
|
); |
|
} else { |
|
// otherwise shouldn't be AsyncFunction |
|
assert.notEqual( |
|
handler.constructor.name, |
|
'AsyncFunction', |
|
`Handler [${handlerId}] accepts a third argument (the 'next" ` + |
|
'callback) but is also an async function. Middleware ' + |
|
'handlers can be either async/await or callback-based. Async ' + |
|
'handler functions should accept maximum of 2 arguments: ' + |
|
'(req, res). Non-async handlers should accept three ' + |
|
'arguments: (req, res, next).' |
|
); |
|
} |
Are you willing and able to fix this?
This probably requires reworking the async chain stuff, so no.
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:
I am aware I could make my handler synchronous, then do
someAsyncWork().then(result => {...})butasync/awaitsyntax was chosen for cleanliness and readability.Cause
node-restify/lib/chain.js
Lines 77 to 101 in 2053ef6
Are you willing and able to fix this?
This probably requires reworking the async chain stuff, so no.