Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function run() {
  2.   const queue = []
  3.   for (let i = 0; i < 100; i++) {
  4.     queue.push(i)
  5.   }
  6.  
  7.   while (queue.length) {
  8.     console.log('loop')
  9.     const items = queue.splice(0, 5)
  10.     await Promise.all(items.map(process))
  11.   }
  12. }
  13.  
  14. async function process(item) {
  15.   await randomDelay()
  16.   console.log(item)
  17. }
  18.  
  19. function randomDelay() {
  20.   return new Promise(resolve => setTimeout(resolve, Math.random() * 100))
  21. }
  22.  
  23. run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement