Created
July 16, 2018 12:27
-
-
Save sj82516/086a19c50cc55ed577e860d5274ebabc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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