On Tue, Mar 29, 2011 at 11:58 AM, Scott Sauyet <[email protected]>wrote:

> Mark McDonnell wrote:
> > Essentially I would like to use Promises in the same way
> > as used by jQuery 1.5 (or at least how their AJAX API uses
> > it) but I just don't have the first clue on how to implement
> > it in actual JavaScript code?
>
> I still think you need to work out the API you would like, perhaps
> something like this would be a good start:
>
>    Lib:
>        when(/*Deferred*/ dfd): Promise
>
>        Deferred:
>            promise(): Promise
>            resolve(/*Whatever*/ args): void, calls 'then' listeners
>            reject(/*Whatever*/ args): void, calls 'fail' listeners
>
>        Promise:
>            then(/*Function*/ fn): Promise, will fire on resolve
>            fail(/*Function*/ fn): Promise, will fire on reject
>
> This might be used like this:
>
>    var dfd = new Lib.Deferred();
>    Lib.when(dfd)
>        .then(function(answer) {console.log(answer);})
>        .fail(function(x) {console.warn("Oops");});
>
>    try {
>        var answer = prompt("What is the meaning of life?");
>        dfd.resolve(answer);
>    } catch (e) {
>        dfd.reject(e);
>    }
>
> That is, I believe, a simplification of jQuery's API.  Is this the
> type of code you're trying to include in your library?
>


Well, not quite. The fail method is redundant as the then method is spec'd
to take both a success and failure callback (jQuery actually extends this to
allow arrays of callbacks for either, but that's unnecessary and outside the
Promises/A spec). Also, I'm not quite sure what you're doing with when here,
but it should take a static library fn, not an instance method.

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to