Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ access permissions and consider creating a new instance per web request.
Create a new `DataLoader` given a batch loading function and options.

- *batchLoadFn*: A function which accepts an Array of keys, and returns a
Promise which resolves to an Array of values.
Promise which resolves to an Array of values or promises which resolve to
values.

- *options*: An optional object of options:

Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/dataloader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ describe('Primary API', () => {
expect(values).toEqual([ 'a', 'b', new Error('Bad Key') ]);
});

it('supports loading with resolved and rejected promises', async () => {
const identityLoader = new DataLoader(keys =>
Promise.resolve(
keys.map(key => (key === 'bad' ?
Promise.reject(new Error('Bad Key')) :
Promise.resolve(key))
)
)
);

const promiseAll = identityLoader.loadMany([ 'a', 'b', 'bad' ]);
expect(promiseAll).toBeInstanceOf(Promise);

const values = await promiseAll;
expect(values).toEqual([ 'a', 'b', new Error('Bad Key') ]);
});

it('batches multiple requests', async () => {
const [ identityLoader, loadCalls ] = idLoader<number>();

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// A Function, which when given an Array of keys, returns a Promise of an Array
// of values or Errors.
export type BatchLoadFn<K, V> =
(keys: $ReadOnlyArray<K>) => Promise<$ReadOnlyArray<V | Error>>;
(keys: $ReadOnlyArray<K>) => Promise<$ReadOnlyArray<V | Promise<V> | Error>>;

// Optionally turn off batching or caching or provide a cache key function or a
// custom cache instance.
Expand Down