Asyncronous Iterators

sb.each(array, callback_loop(item, index, next_method, end_method), callback_end)

Runs next function when "next" method is executed.

const list = ['a', 'b', 'c']

sb.each(list, (item, index, next, end) => {
  console.log(`item: ${item}`)
  setTimeout(next, 3000)
}, () => {
  console.log(`End`)
})
-> item: a // Wait 3 seconds
-> item: b // Wait 3 seconds
-> item: c // Wait 3 seconds
-> End

Call "end" method for break the loop.

const list = ['a', 'b', 'c']

sb.each(list, (item, index, next, end) => {
  console.log(`item: ${item}`)
  setTimeout(() => {
    if (index === 1) end(`Bye!`)
    else next()
  }, 3000)
}, (data) => {
  if (data) console.log(`End: ${data}`)
  else console.log(`End`)
})
-> item: a // Wait 3 seconds
-> item: b // Wait 3 seconds
-> End: Bye!

sb.eachParallelLimit(array, number_limit, callback_loop(item, index, next_method), callback_end)

Runs in parallel limit and next loop when "next" method is executed. Alternative names: eachpl, epl.

var list = ['a', 'b', 'c', 'd']
sb.epl(list, 2, (item, index, next) => {
  console.log(`item: ${item}`)
  setTimeout(next, 2000)
}, () => {
  console.log(`End`)
})
-> item: a
-> item: b
-> item: c // Next to start when next method executed
-> item: d // Next to start when next method executed
-> End

sb.waterfall(array_functions(done, data), callback_end)

Runs next function when "done" method is executed. Alternative name: wf.

sb.waterfall([
  done => {
    console.log(`fire`)
    done(5)
  },
  (done, data) => {
    console.log(`ice: ${data}`)
    done(`win`)
  }
], data => {
  console.log(`End: ${data}`)
})
-> fire
-> ice: 5
-> End: win

Break the waterfall with "true".

sb.wf([
  done => {
    console.log(`fire`)
    done(true, 5)
  },
  (done, data) => {
    console.log(`ice: ${data}`)
    done(`win`)
  }
], data => {
  console.log(`End: ${data}`)
})
-> fire
-> End: 5

sb.parallel(array_functions(done), callback_end(data))

Run functions in parallel and then execute "callback_end".

sb.parallel([
  done => {
    setTimeout(() => {
      console.log(`hi 3!`)
      done(`a`)
    }, 3000)
  },
  done => {
    setTimeout(() => {
      console.log(`hi 2!`)
      done(`b`)
    }, 2000)
  },
  done => {
    setTimeout(() => {
      console.log(`hi 1!`)
      done(`c`)
    }, 1000)
  }
],data => {
  console.log(`End: ${JSON.stringify(data)}`)
})
//Wait 1 second
-> hi 1! //Wait 1 second
-> hi 2! //Wait 1 second
-> hi 3!
-> End: ["a","b","c"]

sb.parallelLimit(number, array_functions(done), callback_end(data))

Run limit of functions in parallel and then execute "callback_end". Alternative name : pl

sb.pl(2, [
  done => {
    setTimeout(() => {
      console.log("go 1!")
      done("a")
    }, 1000)
  },
  done => {
    setTimeout(() => {
      console.log("go 2!")
      done("b")
    }, 3000)
  },
  done => {
    setTimeout(() => {
      console.log("go 3!")
      done("c")
    }, 1000)
  },
  done => {
    setTimeout(() => {
      console.log("go 4!")
      done("a2")
    }, 3000)
  },
  done => {
    setTimeout(() => {
      console.log("go 5!")
      done("b2")
    }, 1000)
  },
  done => {
    setTimeout(() => {
      console.log("go 6!")
      done("c2")
   }, 3000)
  },
  done => {
    setTimeout(() => {
      console.log("go 7!")
      done("a3")
    }, 1000)
  },
  done => {
    setTimeout(() => {
      console.log("go 8!")
      done("b3")
    }, 3000)
  },
  done => {
    setTimeout(() => {
       console.log("go 9!")
       done("c3")
    }, 1000)
  }
],data => {
  console.log(`we: ${JSON.stringify(data)}`)
})
-> go 1!
-> go 3!
-> go 2!
-> go 5!
-> go 4!
-> go 7!
-> go 6!
-> go 9!
-> go 8!
-> we: ["a","b","c","a2","b2","c2","a3","b3","c3"]

sb.forever(callback(repeat, end), callback_end)

Loops syncronous forever. Alternative name: fe.

sb.forever((repeat, end) => {
  console.log(`Hi!`)
  setTimeout(repeat, 3000)
})
-> Hi! // Wait 3 seconds
-> Hi! // Wait 3 seconds
-> ...

Breaking forever loop.

var i = 0
sb.fe((repeat, end) => {
  console.log(`loop: ${i}`)
  if (i>=3) end(`Now Break!!!`)
  i++
  setTimeout(repeat, 3000)
}, data => {
  console.log(`Response: ${data}`)
})
-> loop: 0 // Wait 3 seconds
-> loop: 1 // Wait 3 seconds
-> loop: 2 // Wait 3 seconds
-> loop: 3 // Wait 3 seconds
-> Response: Now Break!!

sb.times(number, callback(index, next, end), end)

Iterates function "number" times.

sb.times(5, (index, next, end) => {
  console.log(`Iterator: ${index}`)
  if (index === 3) end()
  else next()
}, () => {
  console.log(`End!`)
})
-> Iterator 0
-> Iterator 1
-> Iterator 2
-> Iterator 3
-> End!
sb.times(5, (index, next, end) => {
  console.log(`Iterator ${index}`)
  next()
})
-> Iterator 0
-> Iterator 1
-> Iterator 2
-> Iterator 3
-> Iterator 4
-> Iterator 5

sb.for(initial, final, increment, callback(index, next, end), callback_end(data))

Syncronous "for" iterator. Alternative name: forSync.

sb.for(0, 10, 1, (index, next, end) => {
  console.log(index)
  next(5)
}, data => {
  console.log(data)
})
-> 0
-> 1
-> 2
-> 3
-> 4
-> 5
-> 6
-> 7
-> 8
-> 9
-> 10
-> [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ]
sb.for(1, 10, 2, (index, next, end) => {
  console.log(index)
  next(index)
}, data => {
  console.log(data)
})
-> 1
-> 3
-> 5
-> 7
-> 9
-> 11
-> [ 1, 3, 5, 7, 9, 11 ]

results matching ""

    No results matching ""