source: trip-planner-front/node_modules/thunky/index.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.0 KB
Line 
1'use strict'
2
3var nextTick = nextTickArgs
4process.nextTick(upgrade, 42) // pass 42 and see if upgrade is called with it
5
6module.exports = thunky
7
8function thunky (fn) {
9 var state = run
10 return thunk
11
12 function thunk (callback) {
13 state(callback || noop)
14 }
15
16 function run (callback) {
17 var stack = [callback]
18 state = wait
19 fn(done)
20
21 function wait (callback) {
22 stack.push(callback)
23 }
24
25 function done (err) {
26 var args = arguments
27 state = isError(err) ? run : finished
28 while (stack.length) finished(stack.shift())
29
30 function finished (callback) {
31 nextTick(apply, callback, args)
32 }
33 }
34 }
35}
36
37function isError (err) { // inlined from util so this works in the browser
38 return Object.prototype.toString.call(err) === '[object Error]'
39}
40
41function noop () {}
42
43function apply (callback, args) {
44 callback.apply(null, args)
45}
46
47function upgrade (val) {
48 if (val === 42) nextTick = process.nextTick
49}
50
51function nextTickArgs (fn, a, b) {
52 process.nextTick(function () {
53 fn(a, b)
54 })
55}
Note: See TracBrowser for help on using the repository browser.