Skip to content

Commit 923a75f

Browse files
committed
Add example for freezing results with a HOF
Since freezing results is a fairly common request, this illustrates how to provide this behavior in the documentation. Closes #187 Closes #141
1 parent 200b522 commit 923a75f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,21 @@ const usernameLoader = new DataLoader(async names => {
487487
})
488488
```
489489

490+
### Freezing results to enforce immutability
491+
492+
Since DataLoader caches values, it's typically assumed these values will be
493+
treated as if they were immutable. While DataLoader itself doesn't enforce
494+
this, you can create a higher-order function to enforce immutability
495+
with Object.freeze():
496+
497+
```js
498+
function freezeResults(batchLoader) {
499+
return keys => batchLoader(keys).then(values => values.map(Object.freeze))
500+
}
501+
502+
const myLoader = new DataLoader(freezeResults(myBatchLoader))
503+
```
504+
490505

491506
## Common Back-ends
492507

0 commit comments

Comments
 (0)