-
-
Save Almad/8408599 to your computer and use it in GitHub Desktop.
Do not recurse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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