public
Description: Asynchronous library in JavaScript
Homepage:
Clone URL: git://github.com/cho45/jsdeferred.git
name age message
file ChangeLog Thu Nov 19 09:00:38 -0800 2009 update changelog [cho45]
file README.markdown Wed Aug 19 15:05:25 -0700 2009 link [cho45]
file Rakefile Thu Oct 22 03:48:02 -0700 2009 Add Deferred#earlier, Deferred#repeat [cho45]
directory binding/ Tue Oct 20 05:31:21 -0700 2009 $({}).deferred().next ... [hotchpotch]
directory doc/ Thu Oct 22 07:03:21 -0700 2009 doc [cho45]
file jsdeferred.jquery.js Thu Nov 19 08:55:17 -0800 2009 IEでも動くように修正 [SHIMODA Hiroshi]
file jsdeferred.js Thu Nov 19 08:55:17 -0800 2009 IEでも動くように修正 [SHIMODA Hiroshi]
file jsdeferred.nodoc.js Thu Nov 19 08:55:17 -0800 2009 IEでも動くように修正 [SHIMODA Hiroshi]
file jsdeferred.userscript.js Thu Nov 19 08:55:17 -0800 2009 IEでも動くように修正 [SHIMODA Hiroshi]
file makedoc.rb Fri Mar 28 11:45:50 -0700 2008 lang/javascript/jsdeferred/trunk/Rakefile lang/... [drry]
file pre-commit Thu Aug 06 08:30:53 -0700 2009 pre-commit [cho45]
file sample.html Fri Aug 07 20:48:34 -0700 2009 Revert "github pages でうごくように" This reverts com... [cho45]
file test-jsdeferred.js Thu Nov 19 08:55:18 -0800 2009 自動テスト追加 [SHIMODA Hiroshi]
file test-rhino.js Tue Oct 13 09:21:33 -0700 2009 https の修正により Rhino でテストが通っていなかったのを修正 [cho45]
file test.html Fri Aug 07 20:48:34 -0700 2009 Revert "github pages でうごくように" This reverts com... [cho45]
file userscript-test.html Sun Jun 29 06:03:20 -0700 2008 jsonp 追加 [cho45]
README.markdown

JSDeferred

JSDeferred Structure

Simple and clean asynchronous processing.

Sample

JSDeferred Samples

Download

Repository:

git clone git://github.com/cho45/jsdeferred.git

For userscript

Copy and paste following at end of your userscript:

Example:

with (D()) {

next(fun...);

// normal xhr
http.get("...").
next(fun...);

// cross site
xhttp.get("...").
next(fun...);

}

// pasted code
function D () {
...JSDeferred...
}

See binding/userscript.js to get more information of utility functions (http.get/xhttp.get)

Documentation

See source.

Introduction

doc/index.html

Tests

test.html

License

Copyright 2007-2009 cho45 <cho45@lowreal.net>

MIT. See header of jsdeferred.js

Concept

  • Compact
  • Greasemonkey friendly (standalone and compact...)
  • Method chain
  • Short and meaning function names
  • Useful shorthand ways

Internal

This sections use some words as following meanings.

chain::

a sequence of processes.

child::

the Deferred which returns from a callback.

Deferred#foobar::

Deferred.prototype.foobar

Deferred structure and chain structure

A Deferred object has only one callback as its process. Deferred object packages a process (function) as callback and has reference to next Deferred (this is thought like continuation).

Example for understanding Deferred structure.

var d1 = new Deferred();
d1.callback.ok = function () {
    alert("1");
};

var d2 = new Deferred();
d2.callback.ok = function () {
    alert("2");
};

// Set d2 as continuation of d1.
d1._next = d2;

// Invoke the chain.
d1.call();

And example for usual use.

next(function () { // this `next` is global function
    alert("1");
}).
next(function () { // this `next` is Deferred#next
    alert("2");
}).
next(function () {
    alert("3");
});

Deferred#next creates new Deferred, sets the passed functions to process of it, sets it as continuation of this and returns it.

This structure makes easy to chain child Deferreds.

next(function () {
    alert("1");
}).
next(function () {
    alert("2");
    // child Deferred
    return next(function () {
        alert("3");
    });
}).
next(function () {
    alert("4");
});

When the callback returns Deferred, the Deferred calling the callback only sets its continuation (_next) to returned Deferred's continuation.

next(function () {
    alert("1");
}).
next(function () {
    alert("2");
    var d = next(function () {
        alert("3");
    });
    d._next = this._next;
    this.cancel();
}).
next(function () {
    alert("4");
});

After the process, above code is same as following:

next(function () {
    alert("1");
}).
next(function () {
    alert("2");
    next(function () {
        alert("3");
    }).
    next(function () {
        alert("4");
    });
});

Chain child deferred

Error processing and recovering

A Deferred has also error-back for error processing. Let's just see a example (from test):

next(function () {
    throw "Error";
}).
error(function (e) {
    expect("Errorback called", "Error", e);
    return e; // recovering error
}).
next(function (e) {
    expect("Callback called", "Error", e);
    throw "Error2";
}).
next(function (e) {
    // This process is not called because
    // the error is not recovered.
    ng("Must not be called!!");
}).
error(function (e) {
    expect("Errorback called", "Error2", e);
});

The error thrown in callback is propagated by error-back chain. If the error-back returns normal value, the error is considered as recovery, and the callback chain continues.

Difference between MochiKit and JSDeferred

  • MochiKit Deferred has chain by Array. JSDeferred has chain by chain of Deferred.
  • MochiKit Deferred separates parent chain and child chain, JSDeferred not.

Author

Copyright 2007-2009 cho45 <cho45@lowreal.net>