Created
April 9, 2012 02:13
-
-
Save goofmint/2340855 to your computer and use it in GitHub Desktop.
Pot.js demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 関数名を短縮化 | |
Pot.globalize(); | |
begin(function() { | |
debug('BEGIN example'); | |
}).then(function() { | |
debug('BEGIN request'); | |
// JSON を取得 | |
return request('pot.js.example.json', { | |
mimeType : 'text/javascript' | |
}).ensure(function(res) { | |
debug(res); // (Object {XMLHttpRequest ...}) | |
debug('END request'); | |
if (isError(res)) { | |
return { | |
foo : 'Error!', | |
bar : 'Error!', | |
baz : 'Error!' | |
}; | |
} else { | |
debug(res.responseText); // {foo: 'fooValue', bar: 'barValue', baz: 'bazValue'} | |
return parseFromJSON(res.responseText); | |
} | |
// チェイン上でのイテレート | |
}).forEach(function(val, key) { | |
debug(key + ' : ' + val); // foo : fooValue ... etc. | |
return wait(1); // 1 秒間隔で実行 | |
// 0.5 秒 待機して、各チェイン間の実行速度を遅くする | |
}).wait(0.5).speed('slow').then(function(res) { | |
var s = '', keys = []; | |
// 同期処理の forEach | |
forEach(res, function(val, key) { | |
s += key; | |
keys.push(key); | |
}); | |
keys.push(s); | |
return keys; | |
// 次の引数へ分割代入 (Destructuring-Assignment) | |
}).then(function(foo, bar, baz, all) { | |
debug('foo = ' + foo); // foo = 'foo' | |
debug('bar = ' + bar); // bar = 'bar' | |
debug('baz = ' + baz); // baz = 'baz' | |
debug('all = ' + all); // all = 'foobarbaz' | |
return [foo, bar, baz]; | |
// イテレータ関数 map を doze (遅い速度) で実行 | |
}).map.doze(function(val) { | |
debug('in map.doze(val) : ' + val); | |
return val + '!'; | |
}).then(function(res) { | |
debug(res); // ['foo!', 'bar!', 'baz!'] | |
var d = new Deferred(); | |
return d.then(function() { | |
throw new Error('TestError'); | |
}).then(function() { | |
// エラーが発生したため、このコールバック関数は実行されない | |
debug('Help me!!'); | |
}).rescue(function(err) { | |
// エラーをキャッチ | |
debug(err); // (Error: TestError) | |
}).then(function() { | |
// チェインを継続 | |
// reduce によるイテレート (非同期) | |
return Deferred.reduce(res, function(a, b) { | |
return a + b; | |
}).then(function(result) { | |
return result; | |
}); | |
}).begin(); | |
}).wait(2).then(function(res) { | |
debug(res); // 'foo!bar!baz!' | |
}); | |
}).then(function() { | |
debug('END example'); | |
}).end(); // 任意で end によりチェインを閉じることが可能 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment