Guest User

Untitled

a guest
Jul 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. const Dataloader = require("dataloader")
  2. const fakeAsync = (content, t) => new Promise(res => setTimeout(_ => {
  3. console.log("content:", content)
  4. content = content.map(c => c + 1)
  5. res(content)
  6. }, t))
  7.  
  8. async function start() {
  9. const testLoader = new Dataloader(async (keys) => {
  10. console.log("batched by dataloader: ", keys)
  11. let res = await fakeAsync(keys)
  12. return res.map(r => Promise.resolve(r))
  13. })
  14.  
  15. // 1,2,3 should be batch
  16. testLoader.load(1).then(t => console.log("1 got ", t))
  17. testLoader.loadMany([2, 3]).then(t => console.log("[2,3] got ", t))
  18.  
  19. // 4 should be seperate
  20. setTimeout(() => {
  21. testLoader.load(4)
  22. })
  23. Promise.resolve().then(()=>{
  24. testLoader.load(5)
  25. })
  26. }
  27.  
  28. start()
Add Comment
Please, Sign In to add comment