Re:Promises are not neutral enough
by shawndellysse on Friday February 16, 2018 @01:44PM
(#16394705)
Oh I like this. It's pretty simple, both code and conceptually, that it seems obvious in retrospect.
It allows a function to be cancelled from the outside, but only on its own terms, and gives the function a chance to clean up after itself.
I'm going to use this
So you pass the token into a cancellable API, and that API calls token.throwIfRequested() in places where it is safe for it to do so (i.e. outside of critical sections).
A library implementing this in <100 lines of code (based on a spec that has since been sadly abandoned): https://www.npmjs.com/package/cancel-token