async function batchProcess(maxConcurrent, jobs) {
  if (maxConcurrent < 1 || maxConcurrent > jobs.length) {
    maxConcurrent = jobs.length // Run all of them simultaneously if maxConcurrent is beyond reasonable values
  }
  jobs = Array.from(jobs) // Shallow copy, the new array will get mutated

  const out = []
  const errs = []
  async function process() {
    for (let job = jobs.pop(); job; job = jobs.pop()) { // Pop off jobs
      try {
        out.push(await job())
      } catch (e) {
        errs.push(...toArray(e))
      }
    }
  }
  await Promise.all(Array.from({length: maxConcurrent}, () => process())) // Run maxConcurrent calls to process() asyncronously

  if (errs.length) {
    throw errs
  } else {
    return out
  }
}
// maxConcurrent is treated as unlimited if < 1
maxConcurrent is treated as unlimited if < 1
maxConcurrent is treated as unlimited if < 1
async function batchProcess(maxConcurrent, jobs) {
  if (maxConcurrent < 1 || maxConcurrent > jobs.length) {
    maxConcurrent = jobs.length 
  }
  jobs = Array.from(jobs) 
  const out = []
  const errs = []
  async function process() {
    for (let job = jobs.pop(); job; job = jobs.pop()) { 
      try {
        out.push(await job())
      } catch (e) {
        errs.push(...toArray(e))
      }
    }
  }
  await Promise.all(Array.from({length: maxConcurrent}, () => process())) 
  if (errs.length) {
    throw errs
  } else {
    return out
  }
}
async function batchProcess ( maxConcurrent , jobs ) { if ( maxConcurrent < 1 || maxConcurrent > jobs . length ) { maxConcurrent = jobs . length } jobs = Array . from ( jobs ) const out = [ ] const errs = [ ] async function process ( ) { for ( let job = jobs . pop ( ) ; job ; job = jobs . pop ( ) ) { try { out . push ( await job ( ) ) } catch ( e ) { errs . push ( ... toArray ( e ) ) } } } await Promise . all ( Array . from ( { length : maxConcurrent } , ( ) => process ( ) ) ) if ( errs . length ) { throw errs } else { return out } }
maxConcurrent is treated as unlimited if < 1
maxConcurrent is treated as unlimited if < 1
// Run all of them simultaneously if maxConcurrent is beyond reasonable values // Shallow copy, the new array will get mutated // Pop off jobs // Run maxConcurrent calls to process() asyncronously