index.js
function ordered (ondelay) {
  var clk = 0
  var queue = []

  var checking = 0
  function check () {
    if (checking++) return

    setImmediate(function () {
      var o = queue.shift()
      clk = o[1]
      ondelay(function () {
        checking = 0
        if (queue.length) check()
        o[0]()
      }, o[2])
    })
  }

  function delay (fn, ms) {
    var len = queue.length, i = 0
    while (queue.length === len) {
      if (queue[i]) {
        if (queue[i][1] > clk + ms) {
          queue.splice(i, 0, [fn, clk + ms, ms])
        }
      } else queue.push([fn, clk + ms, ms])
      ++i
    }
    check()
  }

  return delay
}
function ordered (ondelay) {
  var clk = 0
  var queue = []

  var checking = 0
  function check () {
    if (checking++) return

    setImmediate(function () {
      var o = queue.shift()
      clk = o[1]
      ondelay(function () {
        checking = 0
        if (queue.length) check()
        o[0]()
      }, o[2])
    })
  }

  function delay (fn, ms) {
    var len = queue.length, i = 0
    while (queue.length === len) {
      if (queue[i]) {
        if (queue[i][1] > clk + ms) {
          queue.splice(i, 0, [fn, clk + ms, ms])
        }
      } else queue.push([fn, clk + ms, ms])
      ++i
    }
    check()
  }

  return delay
}
function ordered ( ondelay ) { var clk = 0 var queue = [ ] var checking = 0 function check ( ) { if ( checking ++ ) return setImmediate ( function ( ) { var o = queue . shift ( ) clk = o [ 1 ] ondelay ( function ( ) { checking = 0 if ( queue . length ) check ( ) o [ 0 ] ( ) } , o [ 2 ] ) } ) } function delay ( fn , ms ) { var len = queue . length , i = 0 while ( queue . length === len ) { if ( queue [ i ] ) { if ( queue [ i ] [ 1 ] > clk + ms ) { queue . splice ( i , 0 , [ fn , clk + ms , ms ] ) } } else queue . push ( [ fn , clk + ms , ms ] ) ++ i } check ( ) } return delay }
one call gets run each turn on setImmediate until none are left.
one call gets run each turn on setImmediate until none are left .