Skip to content

Commit

Permalink
fix throttle mechanism
Browse files Browse the repository at this point in the history
there were two problems:
1) In the throttle case, the callback was being returned, not executed
2) In the case when throttle was not needed, it was neglecting to wrap the result in a Promise.resolve which wasn't causing any problems directly, but it is bad practice to mix return types.
  • Loading branch information
Dustin McQuay committed Jun 23, 2016
1 parent 2b44b52 commit 2689121
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/throttler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ class Throttler {
this._timeBetweenRequestsInMilliSeconds = 1 / this.maxRequestsPerSecond * 1000
this._nextAvailableRequestMillis = getNowMillis()
}

execute(cb) {
let nowMillis = getNowMillis()
if (this.maxRequestsPerSecond === 0) {
return cb()
return Promise.resolve(cb())
} else if (nowMillis >= this._nextAvailableRequestMillis) {
this._nextAvailableRequestMillis = getNowMillis() + this._timeBetweenRequestsInMilliSeconds
return cb()
return Promise.resolve(cb())
} else {
return new Promise((resolve) => {
setTimeout(() => {
resolve(cb)
resolve(cb())
}, this._nextAvailableRequestMillis - nowMillis)
this._nextAvailableRequestMillis += this._timeBetweenRequestsInMilliSeconds
})
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/OperationHelper.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('OperationHelper', () => {
let opHelper = new OperationHelper({
awsId: config.AWS_ACCESS_KEY_ID,
awsSecret: config.AWS_SECRET_ACCESS_KEY,
assocId: config.AWS_ASSOCIATE_ID
assocId: config.AWS_ASSOCIATE_ID
})

return opHelper.execute('ItemSearch', {
Expand Down

0 comments on commit 2689121

Please sign in to comment.