Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Wrapping operations in promises hides important functionality #11

Closed
getify opened this issue May 21, 2018 · 1 comment
Closed

Wrapping operations in promises hides important functionality #11

getify opened this issue May 21, 2018 · 1 comment

Comments

@getify
Copy link
Contributor

getify commented May 21, 2018

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.

@benjamingr
Copy link
Member

Codeified in extras#2 and extras#1 - PRs and other ideas welcome.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants