Skip to content

Instantly share code, notes, and snippets.

@BideoWego
Last active December 6, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BideoWego/733e34de6d5b84835ec9b2b643fbb2f5 to your computer and use it in GitHub Desktop.
Save BideoWego/733e34de6d5b84835ec9b2b643fbb2f5 to your computer and use it in GitHub Desktop.
Calculate and output the estimated time remaining on a large number of batched promises and asynchronous tasks
var NUM_COUNT = 1000;
var _nums;
function _everyFiveWaitASecond() {
var chunk = function(i, results) {
results = results || [];
console.log('chunk(', i, ')');
if (!_nums.length) {
return Promise.resolve();
}
var pair = _nums.pop();
var a = pair[0];
var b = pair[1];
return addAsync(a, b).then(function(res) {
results.push(res);
return delay(
i % 5 === 0 ? 1000 : 0
).then(function() {
return chunk(++i, results);
})
});
};
chunk(0).then(console.log);
}
function _init() {
_nums = [];
for (var i = 0; i < NUM_COUNT; i++) {
_nums.push([i, i + 1]);
}
}
function addAsync(a, b) {
return new Promise(function(resolve) {
var sum = a + b;
var time = Math.floor(Math.random() * 1000);
delay(time).then(function() {
resolve(sum);
});
});
}
function delay(t) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, t);
});
}
function $q(promises) {
return Promise.all(promises);
}
function main() {
_init();
// _everyFiveWaitASecond();
var chunkSize = 5;
var tasks = [];
var results = [];
for (var i = 0; i < _nums.length; i += chunkSize) {
var fn = function(j) {
var chunk = _nums.slice(j, j + chunkSize);
return function() {
return $q(chunk.map(function(pair) {
var a = pair[0];
var b = pair[1];
return addAsync(a, b);
})).then(function(sums) {
results = results.concat(sums);
console.log(sums);
});
};
};
tasks.push(fn(i));
}
var toTimeString = function(ms) {
var seconds = Math.floor(ms / 1000);
var minutes = Math.floor(ms / (1000 * 60));
var hours = Math.floor(ms / (1000 * 60 * 60));
var days = Math.floor(ms / (1000 * 60 * 60 * 24));
var intervals = [];
if (days > 0) {
intervals.push(days, 'days');
}
if (hours > 0) {
intervals.push(hours % 24, 'hours');
}
if (minutes > 0) {
intervals.push(minutes % 60, 'minutes');
}
if (seconds > 0) {
intervals.push(seconds % 60, 'seconds');
}
var str = intervals.join(' ');
return str;
};
var times = [];
var before = Date.now();
var p = tasks.reduce(function(p, task, index) {
return p.then(task).then(function() {
times.push(Date.now());
if (times.length < 2) {
return;
}
var diff = 0;
for (var i = 0; i < times.length - 1; i++) {
var a = times[i];
var b = times[i + 1];
diff += b - a;
}
var avg = diff / times.length;
var ms = avg * (tasks.length - index);
var calculated = toTimeString(ms);
var actual = toTimeString(Date.now() - before);
console.log('Calculated: ', calculated);
console.log('Actual: ', actual);
});
}, Promise.resolve());
p.then(function() {
console.log(results.join(','));
});
}
if (require.main === module) {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment