foreachasync.js/examples/browser/foreachasync-settimeout.js

28 lines
632 B
JavaScript
Raw Normal View History

2014-01-22 20:17:04 +00:00
window.addEventListener('load', function () {
'use strict';
function log() {
document.querySelector('#foreachasync-console').innerHTML +=
'\n' + Array.prototype.join.call(arguments, ' | ');
console.log.apply(console, arguments);
}
var forEachAsync = window.forEachAsync
;
log('i', 'item', 'ms');
2015-01-03 01:08:02 +00:00
forEachAsync([2, 11, 37, 42], function (item, i) {
2014-01-22 20:17:04 +00:00
var ms = Math.floor(Math.random() * 1000)
;
2015-01-03 01:08:02 +00:00
return new Promise(function (resolve) {
setTimeout(function () {
log(i, item, ms);
resolve();
}, ms);
});
2014-01-22 20:17:04 +00:00
}).then(function () {
log('All Done');
});
});