Created
June 3, 2016 06:24
-
-
Save mamor/c090600b47ab48759a675a16befe0745 to your computer and use it in GitHub Desktop.
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
// JSでN個の非同期処理を同期的に実行したい場合の例。 | |
// 1つめの非同期処理が終わったら2つ目に。2つ目が終わったら3つ目に。そして、いくつあるかは場合による。という状況。 | |
var functions = [ | |
function () { | |
var deferred = Q.defer(); | |
// 何らかの処理。遅延してdeferred.resolve()される。 | |
return deferred.promise; | |
}, | |
function () { | |
var deferred = Q.defer(); | |
// 何らかの処理。遅延してdeferred.resolve()される。 | |
return deferred.promise; | |
}, | |
// いくつあるかわからない | |
]; | |
var call = function (fn) { | |
fn().then(function () { | |
var next = functions.shift(); | |
if (next) { | |
call(next); | |
} | |
}); | |
}; | |
call(functions.shift()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment