W3C home > Mailing lists > Public > public-script-coord@w3.org > July to September 2013

Re: Parameter to promise constructor

From: Mark S. Miller <erights@google.com>
Date: Fri, 30 Aug 2013 07:43:55 -0700
Message-ID: <CABHxS9gU+_-zwnhCJ5vZg=sxE3xfUFqWWiKyb_Cx2qCLNEYnRg@mail.gmail.com>
To: Nathan Wall <nathan.wall@live.com>
Cc: Domenic Denicola <domenic@domenicdenicola.com>, "es-discuss@mozilla.org" <es-discuss@mozilla.org>, "www-dom@w3c.org" <www-dom@w3c.org>, "public-script-coord@w3.org" <public-script-coord@w3.org>
Passing around just the resolve function is another way to encode that same
functionality with no loss of generality, since one can call the resolve
with a rejected promise, in order to reject the previously returned
promise. The reject function is just a convenience for that.

(The resolve function by itself is also an attenuation of the resolve
function, though this is not relevant to the current example since it would
be bundled with the resolve function, negating any attenuation.)



On Fri, Aug 30, 2013 at 7:33 AM, Nathan Wall <nathan.wall@live.com> wrote:

> Domenica Denicola wrote:
> > Since there's no real advantage to the `PromiseResolver` approach, and
> there are a number of disadvantages, we were hoping to switch to the
> prevalent `(resolve, reject)` signature in the revised DOM promises spec.
> >
> > Let us know what you think!
>
>
> One advantage to the `PromiseResolver` is that it's easier to pass around
> than two separate functions.  Passing the resolver around isn't common, but
> at my workplace we've made use of it in a "requester" pattern.
>
>
>     function Requester() {
>         this.requests = Object.create();
>     }
>
>     Requester.prototype = {
>         respond: function(requestName, callback) {
>             this.requests[requestName] = callback;
>         },
>         request: function(requestName, ...values) {
>             return new Promise(resolver => {
>                 this.requests[requestName](resolver, ...values);
>             });
>         }
>     };
>
> This allows a general data retrieval mechanism to easily be dropped in
> place.  For instance, if you are writing a Grid class which should display
> tabular data and it needs to request new data any time the page is changed
> or columns or sorted, you can make it a requester.  Then whoever creates a
> Grid can define how it gets its data:
>
>     var grid = new Grid();
>     grid.respond('load-data', function(resolver, page, sorts) {
>         var request_params = {
>             // ...
>             page: page,
>             sorts: sorts,
>             // ...
>         };
>         makeAjaxCall(request_params).then(data => {
>             resolver.resolve(data);
>         });
>     });
>
> Whenever the grid wants to load data, it will internally make a
> "load-data" request.
>
> All of these things could be written to use `resolve` and `reject`
> functions rather than a `PromiseResolver` object, but having one thing to
> pass around has been nice in my experience (we call that thing the
> "Obligation" because if you get it it's your obligation to fulfill the
> promise).
>
> Nathan
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
    Cheers,
    --MarkM
Received on Friday, 30 August 2013 14:44:23 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:37:50 UTC