Skip to content

Instantly share code, notes, and snippets.

@sj82516
Created July 16, 2018 12:27
Show Gist options
  • Save sj82516/086a19c50cc55ed577e860d5274ebabc to your computer and use it in GitHub Desktop.
Save sj82516/086a19c50cc55ed577e860d5274ebabc to your computer and use it in GitHub Desktop.
const Dataloader = require("dataloader")
const fakeAsync = (content, t) => new Promise(res => setTimeout(_ => {
console.log("content:", content)
content = content.map(c => c + 1)
res(content)
}, t))
async function start() {
const testLoader = new Dataloader(async (keys) => {
console.log("batched by dataloader: ", keys)
let res = await fakeAsync(keys)
return res.map(r => Promise.resolve(r))
})
// 1,2,3 should be batch
testLoader.load(1).then(t => console.log("1 got ", t))
testLoader.loadMany([2, 3]).then(t => console.log("[2,3] got ", t))
// 4 should be seperate
setTimeout(() => {
testLoader.load(4)
})
Promise.resolve().then(()=>{
testLoader.load(5)
})
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment