W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2015

Re: Cancellation architectural observations

From: Kevin Smith <zenparsing@gmail.com>
Date: Mon, 2 Mar 2015 11:16:37 -0500
Message-ID: <CA+mDt2wgDPOq5FcfcwQm=qrYwjq4hoaZDndvOH0spu32z3=rtw@mail.gmail.com>
To: Marius Gundersen <gundersen@gmail.com>
Cc: "public-script-coord@w3.org" <public-script-coord@w3.org>, Dean Tribble <tribble@e-dean.com>, es-discuss <es-discuss@mozilla.org>
>
> //this promise has a way to stop what it is doing
> var p1 = new Promise(function(resolve, reject, ignored){
>   var task = setTimeout(resolve.bind(null, "the result"), 1000);
>   ignored(() => clearTimeout(task));
> });
>

Wouldn't the cancellation token strategy be more appropriate here, for all
the reasons that Dean mentioned?  Something like:

    function delay(ms, { cancellation } = {}) {
        return new Promise(resolve => {
            let id = setTimeout(_=> resolve(), ms);
            if (cancellation) cancellation.then(_=> clearTimeout(id));
        });
    }

(Thanks, Dean, for taking the time to write that up BTW.)

//this promise cannot be cancelled, but the result can be ignored
> var p2 = Promise.resolve("the result");
>
> //the callback is not called until next event loop cycle
> p2.then(result => "this is never called");
>
> //the result is ignored, the above callback is never run
> p2.ignore();
>

It seems like what you want here is to "unregister" the callback, similar
to how you would unregister an event listener.  It seems like that only
makes sense on a callback-by-callback basis, since other callbacks for the
same promise might still want to fire.  Can you not handle this use case
with existing API, plus higher order functions?

    let cbs = forgettable();
    p2.then(cbs(result => "this is never called"));
    cbs.forget();
Received on Monday, 2 March 2015 16:17:05 UTC

This archive was generated by hypermail 2.3.1 : Monday, 2 March 2015 16:17:05 UTC