All files / validator/yupSource/util async.js

16.22% Statements 6/37
7.14% Branches 1/14
25% Functions 2/8
20% Lines 6/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53        3x 255x 255x 255x 255x 255x                                                                                      
export function asCallback(promise, callback) {
  promise.then(result => callback(null, result), callback)
}
 
export const once = cb => {
  let fired = false
  return (...args) => {
    Iif (fired) return
    fired = true
    cb(...args)
  }
}
 
export function parallel(fns, cb) {
  let callback = once(cb)
  let count = fns.length
  if (count === 0) {
    return void callback(null, [])
  }
  let results = new Array(count)
 
  for (let i = 0; i < fns.length; i++) {
    let idx = i
    const fn = fns[i]
    fn((err, value) => {
      if (err) return callback(err)
 
      results[idx] = value
      if (--count <= 0) callback(null, results)
    })
  }
}
 
export function settled(fns, cb) {
  let callback = once(cb)
  let count = fns.length
  if (count === 0) {
    return void callback(null, [])
  }
  const results = new Array(fns.length)
  for (let i = 0; i < fns.length; i++) {
    let idx = i
    const fn = fns[i]
    fn((err, value) => {
      results[idx] = err
        ? { fulfilled: false, value: err }
        : { fulfilled: true, value }
 
      if (--count <= 0) callback(null, results)
    })
  }
}