hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling reply.send inside async middleware doesn't stop the chain

faloi opened this issue · comments

Hi @StarpTech. According to the solution you gave me in #240, I implemented a middleware using async/await to early reply a message.

The problem is that the chain doesn't stop there, and the actual response handler gets executed. An example of this can be found at https://repl.it/repls/ScaredPassionateLifecycles:

hemera
    .add({
        topic: 'math',
        cmd: 'add',
        a: Joi.number().required(),
        b: Joi.number().required()
    })
    .use(async function ({ payload }, reply) {
        // I want to return the response here, not in the `end` method.
        reply.send(180)
    })
    .end(async function (req) {
        hemera.log.warn("This should not be called, because we are replying in the middleware")
        return req.a + req.b;
    }
);

With the callback approach this doesn't happen, because you can skip the call to next().

Hi @faloi thanks for reporting. This is a bug. I will provide a fix in #245 please have a look.

Great. I'm using callback style for now, will update when a new release is available.