Proposal: Allow Promise callbacks to be removed

T.J. Crowder tj.crowder at farsightsoftware.com
Tue Apr 24 08:22:44 UTC 2018


On Mon, Apr 23, 2018 at 6:56 PM, Oliver Dunk
<oliver at oliverdunk.com> wrote:
>
> My proposal is that we add a way of removing a particular
> callback, or all callbacks, from a Promise.

As has been raised, this gets complicated quickly when chains come into it.
Also, what if the same function has been used in more than one place in the
chain?

If the simple flag folks have mentioned isn't general enough, I wonder if
you'd be better off with a cancelable *callback*:

```js
function cancelableThen(f) {
    const wrapper = value => f(value);
    wrapper.cancel = () => {
        f = value => value;
    };
    return wrapper;
}

// Using one:

function doSomething(value) {
    // Do something with the value
    return someResult;
}

const cb = cancelableThen(doSomething);
getAPromise().then(cb).catch(handleError);

// Later, you decide you don't need to `doSomething` anymore
cb.cancel();
```

https://jsfiddle.net/nt8tbfvk/1/ <https://jsfiddle.net/nt8tbfvk/>

Simple userland solution. Additional Promise semantics seem like overkill...

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180424/18a2af3e/attachment.html>


More information about the es-discuss mailing list