tc39 / proposal-cancelable-promises

Former home of the now-withdrawn cancelable promises proposal for JavaScript
Other
375 stars 29 forks source link

Missing trifurcation method #33

Closed bergus closed 8 years ago

bergus commented 8 years ago

How can I do what your previous "third state" proposal allowed as .then(onFulfilled, onError, onCancelled)? I believe this functionality is quite essential, and you should provide a utility method for it:

Promise.prototype.trifurcate = function(onFulfilled, onError, onCancelled) {
    return this.then(onFulfilled, e => {
        if ([[CancelBrand]] in e) {
            if (typeof onCancelled == "function") return onCancelled(e);
        } else {
            if (typeof onError == "function") return onError(e);
        }
        throw e;
    });
};

I'm looking for a better name though (bergus/promise-cancellation#4).

domenic commented 8 years ago

You can just write that function yourself. It's already bad practice to use two-arg then, so this is an intentional omission.

bergus commented 8 years ago

Well, I cannot write it myself without Cancel.isCancel (#32), or did I miss something? And no, two-argument then is not a bad practice (dunno who invented that myth), but a very useful tool for control flow.