2

What is the proper way to stop the chain of events on a Node server that was set in motion after it received a request, when that request is cancelled?

E.g.

  • When you press [escape] while a request is done by a browser, the request is aborted.
  • When you call .abort() on a jQuery $.ajax().
  • etc

For just serving some html, this wouldn't be a big deal. The Node server renders some text and nobody listens to the output. Not a big deal.

But when the Node server is actually doing a lot of processing for the response, it would be nice to be able to stop and use the resources for something else.

Ideally some kind of trick to req itself, so I don't have to change whole batches of code to cancel each promise individually.

Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • You can use promise libraries with `.cancel` to write the API to get a similar deferred interface - look into Bluebird. – Benjamin Gruenbaum Mar 29 '14 at 19:01
  • I use [Q](https://github.com/kriskowal/q/wiki/API-Reference) extensively. Anything similar for `Q`? – Redsandro Mar 31 '14 at 15:13
  • Yes I believe Q does this too - check the API docs, but to be honest I'd switch to the much faster Bluebird if I were you. – Benjamin Gruenbaum Mar 31 '14 at 15:31
  • Possible duplicate of [Handling cancelled request with Express/Node.js and Angular](https://stackoverflow.com/questions/35198208/handling-cancelled-request-with-express-node-js-and-angular) – ZachB Feb 01 '18 at 23:01

0 Answers0