You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constonFinished=require('on-finished');constapp=require('express')();app.use((req,res,next)=>{constresult=onFinished(res,()=>/* code */);result===res;// true - just not very usefulnext();});
What could be more useful is:
constonFinished=require('on-finished');constapp=require('express')();app.use((req,res,next)=>{conststopListening=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.
The text was updated successfully, but these errors were encountered:
Hi @nickclaw currently this module's goal is to act as similar to an event emitter interface as possible (which is why we picked the module to have the name on-*, to look like .on( in event emitters. When you attach a listener, it returns the this, which is why we did that here. That at least explains the explanation as to why we are returning the "unless" value of the message back.
That said, the event emitters can remove listeners, but this does not let you--a feature gap I suppose. I would be open to if someone wanted to put together a PR to add this feature: add another export .remove or something similar and then it accepts msg and fn and will remove the fn listener from the given msg, just like .removeListener does on event emitters.
Currently
onFinished
behaves 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
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.The text was updated successfully, but these errors were encountered: