Promises

Kevin Smith khs4473 at gmail.com
Sun Nov 11 05:44:12 PST 2012


> Is the following a counter-example?
>
> On Fri, Nov 9, 2012 at 8:33 AM, Mark S. Miller <erights at google.com> wrote:
>
>> Hi David, thanks for your thoughtful post. I've always used the two-arg
>> form of .then[1], but your post makes a strong case for more often using
>> separate one-arg .then and .fail calls. I say only "more often" because the
>> two arg form can easily make distinctions that the one-arg forms cannot
>>
>>     var p2 = Q(p1).then(val => { throw foo(val); },
>>                         reason => { return bar(reason); });
>>
>> is different than either of the one-arg chainings.
>>
>
I don't think so.  Consider the general two-arg form:

    promise.then(onSuccess, onFail);

We can transform that into a one-arg form like so:

    function WrappedError(err) {
        this.error = err;
    }

    promise
    .fail(err => new WrappedError(err))
    .then(val => val instanceof WrappedError ? onFail(val.error) :
onSuccess(val));

But this involves the creation of an extra (unnecessary) node in the graph.
 And it's obtuse.  I still think the two-arg form makes the most sense as a
base-level "then" API.

- Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121111/a4a8eaa5/attachment.html>


More information about the es-discuss mailing list