Home > other >  What is the best way to have timeout triggers in nodejs ? for example I want to cancel order if no d
What is the best way to have timeout triggers in nodejs ? for example I want to cancel order if no d

Time:02-15

I am building an application similar to uber so I want to have timeout callbacks, Where when a new request comes in I want to keep track of the order and cancel the order after certain time if no drivers accepts the order. I have planned to use setTimeout in nodejs for now, Is there any better way to do this ?

CodePudding user response:

So I assume the proper solution would be:

  1. accept the request.
  2. store it inside the DB with the status pending.
  3. return the response to the client.
  4. in another process or worker search for the requests inside the DB and dispatch them to the drivers, if any driver accepts that request, change the status accepted.
  5. in the meanwhile, you need to have another endpoint to return job status and etc, client use this for checking for job status for let's say 1 minute, and pulls the result every 2 seconds.
  6. Inside the worker just update the request to status canceled if they passed the threshold.
  • Related