Canceling asynchronous operations
Being able to stop a long-running operation is particularly useful if the operation has been canceled by the user or if it has become redundant. In multithreaded programming, we can just terminate the thread, but on a single-threaded platform such as Node.js, things can get a little bit more complicated.
In this section, we will focus on canceling asynchronous operations, not on canceling promises themselves, which is a different topic. It is worth noting that the promises/A+ specification does not define an API for canceling promises. However, if you need this functionality, you can use a third-party promise library such as Bluebird (see more at nodejsdp.link/bluebird-cancelation). Keep in mind that canceling a promise does not automatically cancel the underlying asynchronous operation. In fact, Bluebird offers an onCancel callback in its promise constructor, alongside resolve and reject, which can be used to explicitly cancel the underlying...