looper
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/looper package

4.0.0 • Public • Published

looper

Loop with callbacks but don't RangeError

travis

testling

Synopsis

Normally, if mightBeAsync calls it's cb immediately this would RangeError:

var l = 100000
;(function next () {
  if(--l) mightBeAsync(next)
})

looper detects that case, and falls back to a while loop, in computer science something like this is called a trampoline this module is simpler than other trampoline libraries such as tail-call because it does not preserve arguments. But this is still useful for looping when async recursion is sometimes sync.

This is about 10 times faster than using setImmediate

Example

var looper = require('looper')
 
var l = 100000
var next = looper(function () {
  if(--l) probablySync(next)
})
 
next()

when you want to stop looping, don't call next. looper checks if each callback is sync or not, so you can even mix sync and async calls!

License

MIT

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i looper

    Weekly Downloads

    80,912

    Version

    4.0.0

    License

    MIT

    Last publish

    Collaborators

    • dominictarr