Last active
February 8, 2016 05:38
-
-
Save tyoshikawa1106/7217fa9dd80579d4da55 to your computer and use it in GitHub Desktop.
AngularJSの非同期処理
This file contains hidden or 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
// ひとつ目のPromise | |
function getPromiseA() { | |
var deferred = $q.defer(); | |
console.log('getPromiseA'); | |
setTimeout( | |
function() { | |
var resolveObj; | |
// 処理の成功を示す promise.then() の successCallback をコール ( 引数オブジェクトを渡せる ) | |
console.log('getPromiseA resolve'); | |
deferred.resolve(resolveObj); | |
}, 1000); | |
return deferred.promise; | |
} | |
// 2つ目のPromise | |
function getPromiseB() { | |
var deferred = $q.defer(); | |
console.log('getPromiseB'); | |
setTimeout( | |
function() { | |
var resolveObj; | |
// 処理の成功を示す promise.then() の successCallback をコール ( 引数オブジェクトを渡せる ) | |
console.log('getPromiseB resolve'); | |
deferred.resolve(resolveObj); | |
}, 1000); | |
return deferred.promise; | |
} |
This file contains hidden or 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
function doClick() { | |
var promises = []; | |
var promisesA = getPromiseA(); //任意の非同期処理 A | |
var promisesB = getPromiseB(); //任意の非同期処理 B | |
promises.push(promisesA); | |
promises.push(promisesB); | |
console.log(promises); | |
var promiseAll = $q.all(promises); | |
promiseAll.then( | |
function() { | |
console.log('PromiseAll End'); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment