Skip to content

Instantly share code, notes, and snippets.

@ans-4175
Created January 18, 2016 04:19
Show Gist options
  • Save ans-4175/d7faec67dc6374803bbc to your computer and use it in GitHub Desktop.
Save ans-4175/d7faec67dc6374803bbc to your computer and use it in GitHub Desktop.
Wrap for request using Promise and throttle-exec
var ThrottleEngine = require("throttle-exec")
var Promise = require("when/es6-shim/Promise")
var Request = require("request")
var throttlingCount = 10
var ThrottleInstance = new ThrottleEngine(throttlingCount)
var Engine = function(param){
return ThrottleInstance.registerAction("request",[param])
}
function createRequest(param){
return new Promise(function(resolve,reject){
Request(param,function(err,response,body){
if(!err){
resolve({
response:response,
body:body
})
}else{
reject(new Error(err))
}
})
})
}
ThrottleInstance.registerFunction("request",createRequest)
module.exports = Engine;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment