5

I am using myContract.getPastEvents (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#getpastevents) and I need to be able to cancel this request if a user switches their account or chain. I want to use an abort controller but there is no signal parameter for this request, or any web3 request I have seen.

const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress)
const events = await factory.getPastEvents('BPoolRegistered', {
  filter: {},
  fromBlock: this.startBlock,
  toBlock: 'latest'
})

I have listeners that trigger when a chain or account change occurs and this is when I would call the abort controller in a traditional http request.

UPDATE: While there is no way to abort a web3 request, I found a workaround using abort controllers still. If you wrap your request in a new promise you can add a listener in the promise and reject the promise on abort.

const signal = new AbortController().signal
new Promise((resolve, reject) => {
   signal.addEventListener("abort",()=>{
      return reject(new Error("Aborted"))
    }
   
   //do some stuff
   resolve(something)
})

Just make sure to return after you reject, or return the rejection itself, or else the rest of the code in the promise will still execute.

1 Answer 1

2

Currently GoEthereum will terminate the request only if you terminate the connection to the node, so it is not possible to cancel the request.

However cancelling the request is unwarranted, as each request should only take sub-second load times and you can just discard the answer from a response that is no longer needed.

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.