Re: Cancelable promises

>
>
> > 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.

Received on Sunday, 1 March 2015 15:10:08 UTC