I have an object which can only place 60 API calls per minute. So what I would like to do, is when a function call comes that I know I wont be allowed to place, add it to a queue, and call the function again at a more convenient time.
Heres how I thought to fix it
var API_caller = function(){
this.function_queue = [];
};
API_caller.prototype.make_api_call = function(){
if(this.can_make_call()){
this.make_call();
} else {
// If I cant place an API call then add
// the function to the function queue
this.function_queue.push(this.make_api_call);
}
};
API_caller.prototype.queue_call = function(){
// remove function from queue and call it
var func = this.function_queue.shift();
func();
}
This works fine for functions without parameters but what if make_api_call() had a parameter
API_caller.prototype.make_api_call = function(data){
if(this.can_make_call()){
this.make_call();
} else {
// If I cant place an API call then add
// the function to the function queue
this.function_queue.push(this.make_api_call(data));
}
};
In this case however, make_api_call(data) will be evaluated before it is pushed to function_queue and func will no longer hold a function causing queue_call() to error.
How can I get around this?