load() function batches operations within the current microtask tick. All calls to load() with the same loader function are automatically batched together and executed as a single operation.
Signature
Parameters
A loader function that accepts an array of keys and returns a Promise of values or errors. The loader must return a value (or Error) for each key in the same order.
The key to load. This will be passed to the
loadFn along with other keys collected in the same batch.An optional identity string for deduplication. If not provided, the key will be converted to a string using
jsr:@mr/object-identity. Keys with the same identity will be deduplicated within a batch.Returns
A Promise that resolves to the loaded value for the given key.
How It Works
- When
load()is called, the key is added to a batch for the given loader function - The batch is scheduled to execute after all currently queued microtasks using
queueMicrotask() - Keys with the same identity are deduplicated within the batch
- The loader function is called once with all unique keys
- Each promise resolves with its corresponding value
Ensure that the
loadFn is the same reference between calls. The batching mechanism uses the loader function reference as a key.