2

this is a sample code in web3 document :

var myEvent = myContractInstance.MyEvent({some: 'args'}, {fromBlock: 0, toBlock: 'latest'});

myEvent.watch(function(error, result){ ... });

myEvent.stopWatching();

// extra code to run

The first and second line will have all matching events to undergo the callback function, asynchronously.

If there is more code to run, does stopWatching() method wait until all events complete their callback functions, or do the callback functions run parallel with the extra code?

1 Answer 1

3

This looks to me like a race condition.

The watch callback is executed asynchronously when data matching your filter is polled from the node. Calling myEvent.watch is non-blocking and as such myEvent.stopWatching() will be called immediately afterwards.

The result you will get depends on whether the polling completes in the milliseconds before stopWatching is called.

This is non-deterministic - you might get different results each time you run it.

1
  • When I was testing the upper code, callbacks were called for every events from block 0 to latest, and then the filters were detached. I'm sure it isn't non-deterministic, because I added 1second delay in callback, and .stopWatching() was kindly waiting for minutes for callbacks to finish. Commented Aug 22, 2017 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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