Skip to content

Commit fae38f1

Browse files
LinusUsaihaj
andauthored
fix: add typings and test for priming with promise (#252)
* Add typings and test for priming with promise * add changeset Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent a80c5ff commit fae38f1

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

.changeset/lucky-poets-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"dataloader": patch
3+
---
4+
5+
Fix types for priming cache with promise

src/__tests__/dataloader.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,22 @@ describe('Primary API', () => {
380380
expect(loadCalls).toEqual([ [ 'B' ] ]);
381381
});
382382

383+
it('allows priming the cache with a promise', async () => {
384+
const [ identityLoader, loadCalls ] = idLoader<string>();
385+
386+
identityLoader.prime('A', Promise.resolve('A'));
387+
388+
const [ a, b ] = await Promise.all([
389+
identityLoader.load('A'),
390+
identityLoader.load('B')
391+
]);
392+
393+
expect(a).toBe('A');
394+
expect(b).toBe('B');
395+
396+
expect(loadCalls).toEqual([ [ 'B' ] ]);
397+
});
398+
383399
});
384400

385401
describe('Represents Errors', () => {

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ declare class DataLoader<K, V, C = K> {
5656
* Adds the provided key and value to the cache. If the key already exists, no
5757
* change is made. Returns itself for method chaining.
5858
*/
59-
prime(key: K, value: V | Error): this;
59+
prime(key: K, value: V | PromiseLike<V> | Error): this;
6060
}
6161

6262
declare namespace DataLoader {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class DataLoader<K, V, C = K> {
178178
*
179179
* To prime the cache with an error at a key, provide an Error instance.
180180
*/
181-
prime(key: K, value: V | Error): this {
181+
prime(key: K, value: V | Promise<V> | Error): this {
182182
var cacheMap = this._cacheMap;
183183
if (cacheMap) {
184184
var cacheKey = this._cacheKeyFn(key);

0 commit comments

Comments
 (0)