Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.