factory() function is a convenience method that creates a loader function with a stable reference to your LoadFn. This eliminates the need to pass the loader function on every call.
Signature
Parameters
A loader function that accepts an array of keys and returns a Promise of values or errors.
Returns
A bound function that accepts a key and optional identity, and returns a Promise that resolves to the loaded value.The returned function has the following signature:
Why Use Factory?
Instead of passing the loader function every time you callload(), factory() creates a bound function with the loader already attached. This provides:
- Cleaner API: No need to pass the loader on every call
- Stable Reference: The loader reference is maintained internally
- Better DX: More ergonomic for repeated use throughout your application
Basic Example
Database Example
GraphQL Resolver Example
Comparison with load()
factory() provides a cleaner API when you need to call the loader multiple times.