Pot.Deferred

{Pot.Deferred} new Pot.Deferred ([{Object} options])

Pot.js と PotLite.js で利用可能。

コンストラクタ。Pot.Deferred インスタンスを作成します。

Pot.Deferred コンストラクタ。
新しい Pot.Deferred インスタンスを作成します。

Pot.Deferred を利用することで、非同期処理が簡単に記述できます。
詳しくは、Deferred リファレンス のセクション を参照ください。
new Pot.Deferred() とすることで、新しいインスタンスが得られます。
Pot.globalize() が適応済みの場合、new Deferred() として得られます。
引数 options にオブジェクトを渡すことで、設定が可能です。

options に有効なキー:

async = true {Boolean}

同期、非同期を切り替えます。デフォルトは非同期で true です。
false にすると同期で Deferred チェインが実行されます。
async() メソッドにより、チェインの上で変更できます。

speed = 'normal' {String|Number}

Deferred チェインの速度の設定ができます。
数値で指定するとミリ秒で扱われます。
文字列で指定する場合は、下の定数/値が指定できます。

値 / メソッド名 速度
limp 最も遅い
doze 遅い
slow 遅め
normal 通常
fast 速め
rapid 速い
ninja 最も速い
canceller = null {Function}

任意に cancel() した時に実行される関数を指定します。
また、コンストラクタで指定しない場合でも、 チェイン上で canceller() メソッドを使い関数の登録もできます。

options の指定はすべて任意です。

Pot.Deferred オブジェクト インスタンスを返します。

var d = new Pot.Deferred();

d.then(function() {
  return 100;
}).then(function(res) {
  debug(res); // 100
}).begin();
var d = new Pot.Deferred({
  async : true,
  speed : 'slow'
});

d.then(function(res) {
  debug('Begin');
  debug(res); // 1
  return res + 1;
}).then(function(res) {
  debug(res); // 2
  return res + 1;
}).then(function(res) {
  debug(res); // 3
  // raise an error
  undefinedFunction.call();
}).rescue(function(err) {
  // catch the error
  debug('my error : ' + err); // my error: undefinedFunction is not defined
}).then(function() {
  debug('End.');
}).begin(1);