Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Add Comment
Please, Sign In to add comment