• Stars
    star
    225
  • Rank 177,187 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery style Deferreds for Underscore

Underscore.Deferred

Build Status

v0.4.0 (jQuery 1.8.3)

This is a port of jQuery.Deferred as an Underscore mixin, but it can be used without any depencencies. It currently matches the Deferred specifications and implementation from jQuery 1.8.0, with all the associated helpers.

Deferreds are great, let's take them everywhere

jQuery offers a robust, consistent and well documented API; this project aims to make it portable. jQuery added a handful of helper methods to their implementation of the Common.js Promises/A Spec and they're faithfully reproduced without any dependencies.

Usage

underscore.deferred works on the server and in the browser.

In the browser, just require it like you would any other file. If you're including Underscore on the page, make sure you include it before underscore.deferred. If you don't have Underscore, the plugin attaches to window._.

On the server, simply install via npm and include it with require:

var _ = require('underscore.deferred');
var dfd = new _.Deferred(); // tada!

Or if you'd like to use it as an Underscore or Lodash mixin:

var _ = require('underscore');
_.mixin( require('underscore.deferred') );

Underscore isn't a strict requirement, and assigning it to another name will always work.

Addionally, underscore.Deferred can be used with Ender.js, if you're still into that sort of thing.

Enhancements / Changes

So far, there are only 2 substantial differences between the jQuery API and underscore.deffered's. This may change in the future as new or divergent functionality is tested out, but rest assured that all differences will be called out here (and will have proper test coverage.)

###_.when

underscore.deferred will apply an Array if it's the only argument, providing a useful shortcut for using when with an array of promises. Example:

var form = _.Deferred();
var auth = _.Deferred();

var promises = [ form.promise(), auth.promise() ];

_.when(promises).done(function(){
  // ...
});

form.resolve();
auth.resolve();

###_.then

Inspired by @domenic's Promises/A compliance suite (and the accompanying gist), the 0.4.0 release of underscore.deferred diverges from jQuery's implementation slightly.

First, throwing an error from within a handler in then will reject the deferred object issued by then with the error message. Example:

var dfd = _.Deferred();

dfd.then(function(){
  throw new Error("Oops!");
}).fail(function( err ){
  console.log(err.message); // "Oops!"
});

dfd.resolve();

This behavior alone isn't divergent from jQuery (all tests pass). It's handy for bubbling errors from callbacks without entering callback hell.

Second, when chaining then's or using the new deferred object issued by a call to then the state of the first deferred is not passed to subsequent calls to then. Example:

var dfd = _.Deferred();
dfd.then(null, function( a, b ){
  return a * b;
}).then(function( value ){
  equal(value, 6); // the second deferred is resolved
}, function(){
  // reject handler never called
});

dfd.reject(2, 3); // the first deferred in the chain is rejected

When paired with the first behavior, it makes chaining deferred's even more useful.

Unfortunately, this second point is divergent from jQuery's implementation, with one failing test. jQuery maintains the state of the first deferred object unless the handler returns a new deferred.

Here's an example of the "old" behavior (what's currently in jQuery 1.8.3):

var dfd = _.Deferred();
dfd.then(null, function( a, b ){
  return a * b;
}).then(function(){
  // resolve handler never called
}, function( value ){
  equal(value, 6); // the second deferred is also rejected
});

dfd.reject(2, 3); // the first deferred in the chain is rejected

In the spirit of fidelity to Promises/A, underscored.deferred favors the new behavior.

API

underscore.deferred is an implementation of the jQuery.Deferred API. Here are some of the methods implemented:

For the complete API documentation, see jQuery's Documentation.

This project wouldn't exist if not for the the hard work and effort put into jQuery by its core team and contributors--thanks for making this possible!

Minified Build

One time setup command:

$ npm install

To build with grunt

$ grunt

To run headless Qunit tests (must have phantomjs in your path)

$ grunt qunit

Contributors

Roadmap

The goal is to slim the code footprint for robust deferreds as much as possible while maintaining mixin integration with Underscore and faithfullness to the jQuery API.

This is a work in progress, feel free to contribute.

Release Notes

0.4.0 - adding divergent behavior for _.then

0.3.1 - updating to match jQuery 1.8.3

0.3.0 - adding divergent behavior for _.when

0.2.0 - updating to match jQuery 1.8.0

0.1.4 - updating to match jQuery 1.7.2; Please note that as of 0.1.4 underscore.deferred will be ALL LOWERCASE on npm. The camelcasing was a mistake from the outset, please update your package.json appropriately.

MIT License.

More Repositories

1

backbone.hammer

Hammerjs events for Backbone
JavaScript
74
star
2

grunt-mocha-selenium

Run functional Mocha tests with wd for Selenium, Phantomjs and Appium.
JavaScript
30
star
3

universal-jst

precompiling your templates into JST's, with some sugar
JavaScript
29
star
4

node-gifly

yet another gif.ly
CSS
26
star
5

quack-bot

a bot for quickleft
JavaScript
20
star
6

jquery-tmpl-jst

a compilation script for jqptl on the frontend
JavaScript
17
star
7

gazer

Watch some files, do a thing
JavaScript
15
star
8

wookielight

a limechat theme
CSS
14
star
9

wookiehangover.com

it's sorta like a geocities page
JavaScript
13
star
10

backbone-comments

JavaScript
13
star
11

backbone-in-action-examples

JavaScript
9
star
12

domnode-filestream

a streaming HTML5 FileReader
JavaScript
9
star
13

streamr

streaming file uploads to s3, without touching the disk
JavaScript
7
star
14

pinger

A thing for making sure stuff is up.
JavaScript
7
star
15

node-ramrod

the router you'd write if you were writing a router
JavaScript
7
star
16

gulp-mocha-selenium

Gulp Plugin for running Selenium tests with Mocha and WD
JavaScript
7
star
17

stendig-tab

The iconic Stendig Calendar
JavaScript
6
star
18

unsplash-api

JavaScript
5
star
19

glamor-tachyons

✨ A tool for using Tachyons with Glamor or other CSS-in-JS libraries
JavaScript
5
star
20

pandaco.de

browser based file sharing by way of your favorite editor
JavaScript
5
star
21

phonegap-oauth-example

JavaScript
5
star
22

underscore.cookie

a port of the ever popular $.cookie as an underscore mixin
JavaScript
5
star
23

oblique-tab

Oblique Strategies as your New Tab in chrome
HTML
4
star
24

grunt-mocha-appium

JavaScript
4
star
25

actualcoderwall.com

for the internet points
JavaScript
2
star
26

hapi-sw

sw-precache plugin for Hapi.js
JavaScript
2
star
27

totem-plugin-airplay

Python
2
star
28

jqueryto-2013

Fixing Broken Windows
JavaScript
2
star
29

wd-query

JavaScript
2
star
30

vim-config

Vim Script
2
star
31

stickies

JavaScript
2
star
32

derp-octo-dangerzone

#JIFASNIF
JavaScript
2
star
33

cloudy

uploads to s3 with strata
JavaScript
1
star
34

pantone-bot

Every Pantone color, tweeted
JavaScript
1
star
35

QuickHub

JavaScript
1
star
36

persona-server

JavaScript
1
star
37

scratch

a streaming video server & web front end in node
JavaScript
1
star
38

bugaboo

Tech Throwdown
Ruby
1
star
39

lcthw

C
1
star
40

boulder-js

JavaScript
1
star
41

bombers

roller derby and shit
JavaScript
1
star
42

handlebars-jst

THIS PROJECT HAS MOVED
1
star
43

stoutmonth

stoutmonth.com
JavaScript
1
star
44

slack-prune

Clean up your unused Slack channels!
JavaScript
1
star
45

grapeDrink-for-hackernews

makes outbound links open in new tabs
JavaScript
1
star
46

when.works

doing stuff with gcal availability
CSS
1
star
47

qanvas

A simple loading script for creating and manipulating canvas elements.
JavaScript
1
star
48

test_track

Painless Javascript testing for Rails 3.1
Ruby
1
star
49

react-stendig-calendar

JavaScript
1
star
50

filter-menus

JavaScript
1
star
51

tasker

fun with clojure
Clojure
1
star
52

example-clojure-ring

Clojure/Ring example application for use with OpDemand
Clojure
1
star
53

rdio-playlist

JavaScript
1
star
54

wookie-next

JavaScript
1
star
55

fancy-chat

a coffeescript version of jslatts' nodechat-tutorial
JavaScript
1
star
56

dominode-filestream

1
star