Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2014 20:24
Show Gist options
  • Save anonymous/8407413 to your computer and use it in GitHub Desktop.
Save anonymous/8407413 to your computer and use it in GitHub Desktop.
var https = require('https');
var limit = 2 * 1000;
function fetch(cb) {
var req = https.request('https://url', function(res) {
var text = '';
res.on('data', function(chunk) {
text += chunk;
});
res.on('end', function() {
var body;
try {
body = JSON.parse(text);
} catch (err) {
return cb(err);
}
cb(null, body);
});
});
req.end();
req.on('error', function(err) {
cb(err);
});
}
function run() {
var start = new Date();
fetch(function(err, res) {
handle error
process res
var end = new Date();
var diff = end - start;
if (diff < limit) {
setTimeout(run, limit - diff);
} else {
run();
}
});
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment