Sha256: 53bb4241793f909588dfe40caeaa8bb7b478c10ab45b8f1c726f140557488529

Contents?: true

Size: 1.03 KB

Versions: 35

Compression:

Stored size: 1.03 KB

Contents

'use strict'

var nextTick = nextTickArgs
process.nextTick(upgrade, 42) // pass 42 and see if upgrade is called with it

module.exports = thunky

function thunky (fn) {
  var state = run
  return thunk

  function thunk (callback) {
    state(callback || noop)
  }

  function run (callback) {
    var stack = [callback]
    state = wait
    fn(done)

    function wait (callback) {
      stack.push(callback)
    }

    function done (err) {
      var args = arguments
      state = isError(err) ? run : finished
      while (stack.length) finished(stack.shift())

      function finished (callback) {
        nextTick(apply, callback, args)
      }
    }
  }
}

function isError (err) { // inlined from util so this works in the browser
  return Object.prototype.toString.call(err) === '[object Error]'
}

function noop () {}

function apply (callback, args) {
  callback.apply(null, args)
}

function upgrade (val) {
  if (val === 42) nextTick = process.nextTick
}

function nextTickArgs (fn, a, b) {
  process.nextTick(function () {
    fn(a, b)
  })
}

Version data entries

35 entries across 34 versions & 11 rubygems

Version Path
rapid_stack-0.2.0 templates/frontend/node_modules/thunky/index.js
rapid_stack-0.1.1 templates/FrontEnd/node_modules/thunky/index.js
rapid_stack-0.1.0 templates/FrontEnd/node_modules/thunky/index.js
optimacms-0.1.61 spec/dummy/node_modules/thunky/index.js
disco_app-0.18.0 test/dummy/node_modules/thunky/index.js
disco_app-0.18.2 test/dummy/node_modules/thunky/index.js
disco_app-0.16.1 test/dummy/node_modules/thunky/index.js
disco_app-0.15.2 test/dummy/node_modules/thunky/index.js
disco_app-0.18.4 test/dummy/node_modules/thunky/index.js
disco_app-0.18.1 test/dummy/node_modules/thunky/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/thunky/index.js
disco_app-0.14.0 test/dummy/node_modules/thunky/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/thunky/index.js
tang-0.2.1 spec/tang_app/node_modules/thunky/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/thunky/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/thunky/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/thunky/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/thunky/index.js
tang-0.2.0 spec/tang_app/node_modules/thunky/index.js
tang-0.1.0 spec/tang_app/node_modules/thunky/index.js