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?
-- Scott
--
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]