Complete the code to create a new DataLoader instance.
const userLoader = new DataLoader([1]);The DataLoader constructor requires a batchLoadFn function that batches and returns results.
Complete the batch loading function to accept keys and return a Promise of results array.
function batchLoadFn([1]) { return Promise.resolve(keys.map(key => getUserById(key))); }
The batch loading function receives an array called keys representing requested IDs.
Fix the error in the batch function to correctly return results in the same order as keys.
function batchLoadFn(keys) {
const users = fetchUsersByIds(keys);
return [1];
}DataLoader requires the batch function to return a Promise resolving to an array of results matching the keys order.
Fill both blanks to create a DataLoader and use it to load a user by ID.
const userLoader = new DataLoader([1]); userLoader.[2](123).then(user => console.log(user));
get instead of load.The DataLoader is created with the batch loading function, and load is called to get a user by ID.
Fill all three blanks to implement caching in DataLoader and clear cache for a user ID.
const userLoader = new DataLoader({
batchLoadFn: batchLoadFn,
cache: [1]
});
userLoader.[2](456);
userLoader.[3](456);remove instead of clear.Setting cache: true enables caching. Use load to fetch and clear to remove cache for a key.