0

My code is the following:

contract.events.Deposit({}, function (err, event) {
    if (err) {
        console.error;
    } else {
        console.log(event.blockNumber);
    }
})
    .on('data', function (event) {
        console.log(event);
    })

What I want to do is to close the event listener after some condition is fulfilled. How can I do that?

2

The web3.js 1.0 documentation states that the return value of creating an event is an EventEmitter. To close the event, you have to remove all of the EventEmitter's listeners using the bound event's names:

// set up emitter:
let emitter = contract.events.Deposit({}, function (err, event) {
    if (err) {
        console.error;
    } else {
        console.log(event.blockNumber);
    }
})
.on('data', function (event) {
    console.log(event);
})

// ==== //

//Somewhere else in your code where you have access to the emitter:
// Close emitter:
emitter.removeAllListeners('data');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.