1

I am using the JavaScript API in my truffle tests to get the events from the contract: https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-allevents

          const allEvents = instance.allEvents({
            fromBlock: 0,
            toBlock: 'latest'
          });
          allEvents.watch((err, res) => {
            console.log(res);
          });

It never stops listening so I tried adding this to the end:

          allEvents.stopWatching()

The listening stops, but nothing is ever logged, so I tried adding a delay:

          setTimeout(allEvents.stopWatching, 1000);

But the listening doesn't stop.

How do I stop listening after all the events emitted till now have been retrieved?

  • What version of web3 are you using? Which condition should cause to stop watching all events? If you call .stopWatching() immediately after .watch() it will stop there before any event, setTimeout might not work because allEvents.stopWatching is a function without a binding. – Ismael Sep 18 '18 at 15:27
  • web3 Version '0.20.6' included in Truffle. The condition that should cause it to stop watching is that it got all the past events, but I don't know how to identify that. – Harsha Laxman Sep 22 '18 at 15:43
0

It says that if you pass a callback function you don't need to call stopWatching(). The problem is somewhere else so just abandon the idea of using stopWatching. Could you try this implementation instead:

const allEvents = instance.allEvents({
        fromBlock: 0,
        toBlock: 'latest'
      });
      allEvents.watch((err, res) => {
        if (!error)
        console.log(res);
      });

Give me an update

  • Same as before, doesn't stop listening – Harsha Laxman Sep 17 '18 at 1:05

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.