Friday, 28 April 2017

Function execution queue based on parameters

I'm struggling to find a solution for this problem. The use case is as follow:

  • I have a function to retrieve a resource from an external location
  • Quite often the call to this function n number of times concurrently
  • When this happen I would like all the function call to "wait" until the first function returns and just use the output of that function
  • The crux of the issue here that the function is called 10 times a the same time

Say for example the function is:

function retrieveResource(resourceId, callback) {
  extLib.getResource(resourceId, function (err, data) {
    callback(err, data);  
  };
};

So when multiple "caller" are calling retrieveResource functions, often they have the same resourceId. When this happen within say 1 second interval, I just want to have 1 call to extLib.getResource executed and the rest will just use the same value as the first function call.

Using cache doesn't quite work out here because the function is called roughly at the same time, they all check the cache roughly at the same time and the cache is empty at that point.

I looked at _.debounce, _.throttle and they are not what I'm looking for since debounce is executing just one function and throttle is still executing all functions.

Any ideas?



via E45 Carbomer

No comments:

Post a Comment