Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active May 29, 2016 04:37
Show Gist options
  • Save nishinoshake/7b0e0e1d7b99e75b4fba to your computer and use it in GitHub Desktop.
Save nishinoshake/7b0e0e1d7b99e75b4fba to your computer and use it in GitHub Desktop.
Deferredによる非同期処理
Deferred
順番待ち的な処理が簡単に実装できる。
1.Deferredオブジェクトを生成してとりあえず返す(return d.promise())
2.処理が正常終了したら合図を返す(d.resolve())
3.失敗しても合図を返す(d.reject())
4.thenでつなげると前段の状態変化を待って実行できる
(then()はdone()とfail()をまとめて実行できる)
function sample() {
df().then(function() {
});
}
function df() {
var d = new $.Deferred;
$("sample").fadeIn(200, function() {
d.resolve(); //d.reject('Error!!');
});
return d.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment