Tuesday 16 May 2017

Stopping an event listener after a condition

I have a code similar to this:

emitter.on('request', request => {
    if(condition) doSomething(request);
});

function doSomething(request){
    emitter.on('message', message => {
        if(anotherCondition){
            //
            //Some code to send something
            //
        }
    })
}

Basically, if that's not clear, I'm trying to have a listener wait for a request then send back whatever and wait for the receiver's response to that whatever I sent. However, when this happens, the on('message' listener is still active and the next request the emitter gets it sends the the whatever twice. The next time, it sends the whatever three times. How do I safely stop this?



via Wright

No comments:

Post a Comment