1

I'd like to set timeout/deadline for operations from Firestore Node.js library (like getting a document), because it sometimes takes too long (even more than several minutes). I'd like to set it to ~20-30 seconds.

Is it possible to set timeout/deadline for operations from Firestore Node.js library?

Adrian
  • 11
  • 1

1 Answers1

0

I think this would be a little tricky, more than likely what you would need to do is use the Repository pattern, and set your own promises, and manage the timings of those requests. If it goes beyond 20 seconds, cancel the request and handle the error back to the user.

Once you have the architecture in place, you could provide callbacks, something like:

this.query(() => { // firestore query here })

Your query method would create a custom promise object, and wait on the firestore query. If it takes too long, it would cancel the request.

Alternatively, you could setup two promises, one being the Firestore request, the other being an awaitTimeout promise.

Then, using promise.any, you could wait for the first response back - if it's the firestore query, cancel your await. If it's the await, cancel the firestore query.

More details here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any

Oddman
  • 3,715
  • 1
  • 16
  • 13
  • Thanks @Oddman for your reply! I was investigating this approach and one can even use https://nodejs.org/api/globals.html#class-abortcontroller instead of Promise.any/Promise.race. But how can I cancel the query? I haven't noticed anything like this in Firestore docs. – Adrian Jul 24 '23 at 10:32
  • You have to use custom promise implementations (as ES6 does not yet support cancellable promises), but doing that you essentially wrap your firestore queries in those promise objects, as I said in the response. You have to architect a system around it, unfortunately. – Oddman Jul 25 '23 at 13:18
  • More details here, btw: https://stackoverflow.com/questions/29478751/cancel-a-vanilla-ecmascript-6-promise-chain – Oddman Jul 25 '23 at 23:59