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
}