Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function run() {
- const queue = []
- for (let i = 0; i < 100; i++) {
- queue.push(i)
- }
- while (queue.length) {
- console.log('loop')
- const items = queue.splice(0, 5)
- await Promise.all(items.map(process))
- }
- }
- async function process(item) {
- await randomDelay()
- console.log(item)
- }
- function randomDelay() {
- return new Promise(resolve => setTimeout(resolve, Math.random() * 100))
- }
- run().catch(console.error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement