Skip to content

Instantly share code, notes, and snippets.

@Almad
Forked from anonymous/example.js
Last active January 3, 2016 04:19
Show Gist options
  • Save Almad/8408599 to your computer and use it in GitHub Desktop.
Save Almad/8408599 to your computer and use it in GitHub Desktop.
Do not recurse
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 {
process.nextTick(run);
}
});
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment