Last active
March 16, 2021 12:43
-
-
Save noriaki/1de0730ab783b47be216 to your computer and use it in GitHub Desktop.
Promise.allがブラウザ環境で並列実行されているか観察するコード via: http://ja.stackoverflow.com/questions/10363
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
console.profile('Promise.all'); | |
console.time('Promise.all'); | |
var obj = [ | |
{a:"abc",b:123,c:true}, | |
{a:"efg",b:456,c:true}, | |
{a:"hij",b:789,c:false} | |
]; | |
var promises = obj.map(function(data) { | |
return new Promise(function(resolve, reject) { | |
// ここに処理 | |
var delay = Math.floor(Math.random() * 5000); /* ランダムに0〜5秒 */ | |
setTimeout(function() { | |
console.log('processing:', delay + '[msec]', data); | |
// 完了(成功)時には resolve() を、失敗時には reject() を呼ぶ | |
resolve(data); | |
}, delay); | |
}); | |
}); | |
Promise.all(promises).then(function(results) { | |
console.log('results:', results); | |
console.timeEnd('Promise.all'); | |
console.profileEnd('Promise.all'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment