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.
Currently
onFinishedbehaves like this:What could be more useful is:
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
onFinishedlisteners that need to stop listening, it's cleaner to give them a way to clean themselves up instead of just sitting in memory.