Skip to content

异步并发控制 #86

Description

@inkjuncom
async function loadImages(urls, max = 3) {
  const results = []
  const executing = new Set()

  for (const url of urls) {
  
   if (executing.size >= max) {
     await Promise.race(executing)
   }

    const promise = loadImage(url)
      .then(res => {
         results.push(res)
      ))
      .finally(() => {
        executing.delete(promise)
      })
    
    executing.add(promise)
  }
  
  await Promise.all(executing)
  return results
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions