Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Created May 23, 2018 13:06
Show Gist options
  • Save pyldin601/db1968f3db2e2b183ae038faf0928cef to your computer and use it in GitHub Desktop.
Save pyldin601/db1968f3db2e2b183ae038faf0928cef to your computer and use it in GitHub Desktop.
import * as PromiseThrottle from 'promise-throttle';
function makeThrottle(requestsPerSecond: number) {
const promiseThrottle = new PromiseThrottle({ requestsPerSecond });
return (weight: number = 1) => (target: any, key: string, descriptor: PropertyDescriptor) => {
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, key);
}
const original = descriptor.value;
descriptor.value = function(...args) {
return promiseThrottle.add(() => original.apply(this, args), weight);
};
};
}
export default makeThrottle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment