Skip to content

Return listener-remover instead of response. #16

@nickclaw

Description

@nickclaw

Currently onFinished behaves like this:

const onFinished = require('on-finished');
const app = require('express')();

app.use((req, res, next) => {
    const result = onFinished(res, () => /* code */);
    result === res; // true - just not very useful
    next();
});

What could be more useful is:

const onFinished = require('on-finished');
const app = require('express')();

app.use((req, res, next) => {
    const stopListening = onFinished(res, () => /* code */);

    doSomeOperation(err => {
        if (err) {
            stopListening();
            res.sendStatus(500); // onFinished listener is not called
        } else {
            next();
        }
    });
});

My actual use case is a little more complicated, (e.g. I can't just start listening if there isn't an error) and I know you could do this with a flag, but I'd argue in the case where you might have lots of onFinished listeners that need to stop listening, it's cleaner to give them a way to clean themselves up instead of just sitting in memory.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions