#Simply Deferred ###Simplified jQuery Deferred API for Node and the browser
##Installation npm install simply-deferred
If you're on a browser, simply include https://raw.github.com/sudhirj/simply-deferred/master/deferred.min.js
##Usage
var Deferred = require('simply-deferred').Deferred;
var rendering = new Deferred();
rendering.done(function(){
console.log('Finished rendering');
});
//...
rendering.resolve();
##API Simply Deferred is partially compatible with jQuery's API, so the docs and usage are the same, except that they're restricted to the following methods:
Deferred()
deferred.state()
deferred.done()
deferred.fail()
deferred.always()
deferred.promise()
deferred.resolve()
deferred.reject()
deferred.pipe()
deferred.then()
when()
In my experience, these methods cover over 90% of all use cases. I've also decided to drop resolveWith
and rejectWith
because CoffeeScript and most, if not all JS libraries now provide easier ways to pre-bind your functions. This is allowed the code to be far simpler, smaller and better tested.
###Usage with Zepto
Simply Deffered also acts as a plugin to Zepto. The absence of a Deferred library was one of the biggest reasons I've been holding back, so I thought it made sense to write one. Once you have both Zepto and Simply Deferred on your page, just do Deferred.installInto(Zepto)
to set it up. The installation makes the following changes to bring it closer to jQuery:
- Aliases the
Deferred
constructor to$.Deferred
. - Aliases the
when
method to$.when
. - Wraps
$.ajax
to return apromise
, which has only the following methods:state()
,done()
,fail()
andalways()
. The arguments passed to thedone
andfail
callbacks are the same ones passed to thesuccess
anderror
options.