rsvp-tools is a set of utility functions for wrapping event or callback based JavaScript APIs in RSVP promises. Both the browser and Node.js environments are supported.
A simple example to get started. Here is a simple API call on the Facebook JavaScript SDK:
FB;
With rsvp-tools:
// Wrap the `api` function in promisesvar api = rsvpToolscallback; ;
A simple example doesn't leverage promises very much- in this more complicated example we will find out which is more popular, my hometown or my current town.
var { ;}FB;
This amount of indirection can make following source code difficult. rsvp-tools can help you clean it up:
var api = rsvpToolscallback; ;
Usage
// callback.build//// Wrap async expecting a success and error callback after other// arguments. Use this wrapper for dealing with a context that will// never change.//var facebookApi = rsvpToolscallback; // Always calling `api` on the `FB` context;
// callback.contextPassed//// Wrap async expecting a success and error callback after other// arguments. Use this wrapper for dealing with a context that will// be decided at call-time.//var facebookApi = rsvpToolscallback; // Apply the promise-wrapped `api` method to the passed FB object.;
// eventd.contextPassed//// Wrap async emitting success/failure events. This wrapper expects// the context to be provided at call-time.//var open = rsvpToolsevented; // Apply `open` to the XMLHTTPRequest instance.; // evented.contextPassed will try to attach handlers using several// standard functions://// addEventListener, attachEvent, on//// And will listen for the events "success" and "error". These can// be changed at setup-time.var readAsDataUrl = rsvpToolsevented; // Promise resolves on the `load` event
Building
npm install
bower install
To build:
grunt
And to run tests start the server with:
grunt server
And visit http://localhost:8000/test/
rsvp-tools is available under the MIT license. Cory Forsyth and Matthew Beale 2013