memoize(fn, hasher) in the Async module caches the results of an async function using hashing.
When creating a hash to store function results against, the callback is omitted from the hash and an optional hash function can be used.
Note: If the async function produces an error, the result will not be cached and subsequent calls will call the wrapped function.
memoize(fn, hasher)
fn: The async function to proxy and cache results from.hasher: An optional function for generating a custom hash for storing results. It has all the arguments applied to it, apart from the callback, and must be synchronous.The function returns a memoized version of fn.
The cache of results is exposed as the memo property of the function returned by memoize.
Note: If no hash function is specified, the first argument is used as a hash key. In case you are using objects and arrays as the first argument, you should specify your own hash function.
In the following example, we will create a memoized version of an async function slow_fn and store it as fn. fn can, later on, be used as if it were slow_fn. You can access the cache of results using fn.memo.
// An async functionvar slow_fn = function(name, callback) {// do somethingcallback(null, result);};// fn is the memoized version of slow_fnvar fn = async.memoize(slow_fn);// fn can now be used as if it were slow_fnfn('some name', function() {// callback})
Free Resources