File size: 617 Bytes
5ae7e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function promiseAllStepN (n, list) {
  let tail = list.splice(n)
  let head = list
  let resolved = []
  let processed = 0
  return new Promise(resolve => {
    head.forEach(x => {
      let res = x()
      resolved.push(res)
      res.then(y => {
        runNext()
        return y
      })
    })
    function runNext () {
      if (processed == tail.length) {
        resolve(Promise.all(resolved))
      } else {
        resolved.push(tail[processed]().then(x => {
          runNext()
          return x
        }))
        processed++
      }
    }
  })
}

module.exports = n => list => promiseAllStepN(n, list)