You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
Certain libraries and even now Node support automatically wrapping a callback-expecting utility to be a promise-returning utility. One common use case is when you want a promisfied setTimeout(..).
The problem is, a promise only represents the completion (or early termination/rejection) of such an operation, and gives you no way to signal that you actually want some way to manually control the operation (timer), such as clearing a timer.
What I'm getting at is, the biggest frustration I think people have with promises is that they only represent observation of a result, not control over the operation producing the result. And yet, many many use-cases, including cancelation, imply that what developers want is both control and observation.
In fact, these are separate capabilities, and there's lots of historical precedent for why we shouldn't try to conflate them into a single capability (no cancelable promises).
Instead, more utilities and operations should be offering a way not to just "promisify" a utility, but also (optionally) to get a controller for that operation as well.
For example, I think an async function should never have returned a promise, but instead returned an object with both the promise and a cancel() method. This object return value would include both capabilities, but standard practice would be to separate them (via destructuring, etc), and thus you'd pass around the promise to any parts of the application that want to merely observe the results, and separately pass around the cancel() to any parts who need to control the operation (cancel it, for example). This object would be similar to the iterator returned from calling generators.
Unfortunately, JS shipped async functions without this capability, which is why we need to explore the next best thing for cancelation: cancelation tokens. I built CAF for that purpose.
But coming back around to my point: I think we need utilities that allow you to produce this controller object from callback-wrapping instead of only producing promises.
The text was updated successfully, but these errors were encountered:
Certain libraries and even now Node support automatically wrapping a callback-expecting utility to be a promise-returning utility. One common use case is when you want a promisfied
setTimeout(..)
.The problem is, a promise only represents the completion (or early termination/rejection) of such an operation, and gives you no way to signal that you actually want some way to manually control the operation (timer), such as clearing a timer.
What I'm getting at is, the biggest frustration I think people have with promises is that they only represent observation of a result, not control over the operation producing the result. And yet, many many use-cases, including cancelation, imply that what developers want is both control and observation.
In fact, these are separate capabilities, and there's lots of historical precedent for why we shouldn't try to conflate them into a single capability (no cancelable promises).
Instead, more utilities and operations should be offering a way not to just "promisify" a utility, but also (optionally) to get a controller for that operation as well.
For example, I think an
async function
should never have returned a promise, but instead returned an object with both the promise and acancel()
method. This object return value would include both capabilities, but standard practice would be to separate them (via destructuring, etc), and thus you'd pass around the promise to any parts of the application that want to merely observe the results, and separately pass around thecancel()
to any parts who need to control the operation (cancel it, for example). This object would be similar to the iterator returned from calling generators.Unfortunately, JS shipped
async function
s without this capability, which is why we need to explore the next best thing for cancelation: cancelation tokens. I built CAF for that purpose.But coming back around to my point: I think we need utilities that allow you to produce this controller object from callback-wrapping instead of only producing promises.
The text was updated successfully, but these errors were encountered: