Skip to content

Instantly share code, notes, and snippets.

@bcoe
Created June 20, 2014 20:16
Show Gist options
  • Select an option

  • Save bcoe/12c1d3a7c83f979c67d7 to your computer and use it in GitHub Desktop.

Select an option

Save bcoe/12c1d3a7c83f979c67d7 to your computer and use it in GitHub Desktop.
// var packages = ['thumbd', 'underscore', 'request'];
var async = require('async');
function IssuePopulator(packages) {
this.interval = 1000; // try to grab more packages every second.
this.workSize = 10;
this.packages = packages;
};
IssuePopulator.prototype.doWork = function() {
var _this = this;
console.log('doing a block of work.')
if (this.packages.length) {
// terminal condition.
console.log('we finished grabbing all the packages.');
} else {
async.parallel(this.getWork(), function(err) {
if (err) console.log(err);
setTimeout(function() {
_this.doWork();
}, _this.interval);
})
}
};
IssuePopulator.prototype.getWork = function() {
var _this = this,
work = [];
this.packages.splice(0, this.workSize).forEach(function(packageName) {
work.push(
function(done) {
_this.getGitHubInfo(packageName, done);
}
)
});
return work;
};
IssuePopulator.prototype.getGitHubInfo = function(packageName, done) {
// do all the apis call you need to.
// call done once all of them are done in sequenece().
// you could use async.series, or you could use a bunch of closures.
};
// code elsewhere in your program loads `packages`.
(new IssuePopulator(packages)).doWork();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment