Created
January 18, 2016 04:19
-
-
Save ans-4175/d7faec67dc6374803bbc to your computer and use it in GitHub Desktop.
Wrap for request using Promise and throttle-exec
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 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