Skip to content

Instantly share code, notes, and snippets.

@nameofname
Last active November 18, 2019 23:09
Show Gist options
  • Save nameofname/97556ec661c26559faa7bc00747751e1 to your computer and use it in GitHub Desktop.
Save nameofname/97556ec661c26559faa7bc00747751e1 to your computer and use it in GitHub Desktop.
Testing out batch functionality with dataloader
const DataLoader = require('dataloader');
const data = [
{id: 1},
{id: 2},
{id: 3},
];
const myLoader = new DataLoader(keys => {
const results = keys.map(key => data[key - 1]);
console.log('INVOKED! What are the keys?', keys);
return Promise.resolve(results);
});
async function execute() {
const [one, two, four, twoAgain, twoAThirdTime] = await Promise.all([
myLoader.load(1),
myLoader.load(2),
myLoader.load(4),
myLoader.load(2),
myLoader.load(2),
]);
console.log([one, two, four, twoAgain, twoAThirdTime]);
const [oneAgain, three] = await Promise.all([
myLoader.load(1),
myLoader.load(3),
]);
console.log([one, two, three, four, oneAgain]);
}
execute();
/**
Output :
INVOKED! What are the keys? [ 1, 2, 4 ]
[ { id: 1 }, { id: 2 }, undefined, { id: 2 }, { id: 2 } ]
INVOKED! What are the keys? [ 3 ]
[ { id: 1 }, { id: 2 }, { id: 3 }, undefined, { id: 1 } ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment