Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noriaki/1de0730ab783b47be216 to your computer and use it in GitHub Desktop.
Save noriaki/1de0730ab783b47be216 to your computer and use it in GitHub Desktop.
Promise.allがブラウザ環境で並列実行されているか観察するコード via: http://ja.stackoverflow.com/questions/10363
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