Cancelable promises

Kevin Smith zenparsing at gmail.com
Sun Mar 1 07:09:35 PST 2015


>
>
> > 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) {
            doCleanup();
            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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150301/6f3b5e1c/attachment.html>


More information about the es-discuss mailing list