News for hackers. Stuff that matters.
Re:Promises are not neutral enough by rictic on Thursday February 15, 2018 @01:47PM (#16386447)
Neither lazy nor eager is neutral. Sometimes you want one, sometimes you want the other, and you can build either out of the other.

For promise cancellation, this has been talked to death, but in short, making any function preemptable at any point in its execution makes writing correct code much much harder. As an example, I've got an API that takes independently cancellable requests. Multiple requests often need to calculate the same thing, so there's a cache. Any given promise in the system might be downstream of multiple requests. If cancellation is built into promises, how do I express how cancellation should propagate through the tree of promises?

A C#-style cancellation token API, orthogonal to promises, is simple, easy to build, and easy to understand.

Re:Promises are not neutral enough by mnarayan01 on Thursday February 15, 2018 @02:19PM (#16386685)
> A C#-style cancellation token API, orthogonal to promises, is simple, easy to build, and easy to understand.

The problem is that promises were added to the language without any of that being hashed out. Now you may say "Having language-level support for promises is useful; cancellation tokens are more library details", and you'd have a point. The flip-side, however, is that e.g. the Fetch API is only now getting any sort of cancellation ability, and currently I believe it's limited to Firefox and Edge.

Which is not to say that I'm even confident that language support for Promises should have been held up. But I understand the complaints.

Re:Promises are not neutral enough by shawndellysse on Thursday February 15, 2018 @03:50PM (#16387498)
> A C#-style cancellation token API, orthogonal to promises, is simple, easy to build, and easy to understand.

I'm interested in learning more about this, do you happen to have any links to building such an api?

Re:Promises are not neutral enough by rictic on Thursday February 15, 2018 @04:11PM (#16387668)
Creating a cancel token gives you two things: a token and a function. You call the function when the token should be cancelled, and the token can tell you when it has been cancelled. The simplest way to query the token is to call token.throwIfRequested(), which throws a Cancel if the token is cancelled. The token can also give you promises or callbacks of cancellation if you like, so you can do stuff like `const result = await Promise.race(token.promise, promiseOfResult);`

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

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