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

Re: Cancelable promises

From: Kevin Smith <zenparsing@gmail.com>
Date: Sun, 1 Mar 2015 10:09:35 -0500
Message-ID: <CA+mDt2y+uv6DhLXhPhHK_A4trrDfmT85C56TUCdTEAcNJLNVjA@mail.gmail.com>
To: Domenic Denicola <d@domenic.me>
Cc: Anne van Kesteren <annevk@annevk.nl>, "public-script-coord@w3.org" <public-script-coord@w3.org>, es-discuss <es-discuss@mozilla.org>
> > So again the question is: can we come up with a cancellation-token-style
> pattern which is JS-ergonomic?
> I tried to discuss some stuff in that direction at
> https://github.com/slightlyoff/ServiceWorker/issues/625#issuecomment-75342617
I think using a promise in place of a "token is a great idea.  So a
cancelable async function might look like:

    async function af(cancel) {
        let abort = false;
        cancel.then(_=> abort = true);
        await op1();
        if (abort) {
            return something;
            // What is "something" here?
            // Should we return what cancel resolves with?
            // Should it throw/reject instead?
        await op2(cancel); // Send cancellation to a nested task
        // etc.

Like you mention in the github issue, the task won't be able to immediately
tell if cancel is already-resolved, but I think that's fine.  If we're
already cancelled, then we shouldn't call the async function to begin with.

Looks pretty good to me, although there are a few details that would need
to be ironed out.
Received on Sunday, 1 March 2015 15:10:08 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 17:14:24 UTC