Last change
on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | var nextTick = nextTickArgs
|
---|
| 4 | process.nextTick(upgrade, 42) // pass 42 and see if upgrade is called with it
|
---|
| 5 |
|
---|
| 6 | module.exports = thunky
|
---|
| 7 |
|
---|
| 8 | function 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 |
|
---|
| 37 | function isError (err) { // inlined from util so this works in the browser
|
---|
| 38 | return Object.prototype.toString.call(err) === '[object Error]'
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | function noop () {}
|
---|
| 42 |
|
---|
| 43 | function apply (callback, args) {
|
---|
| 44 | callback.apply(null, args)
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | function upgrade (val) {
|
---|
| 48 | if (val === 42) nextTick = process.nextTick
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | function nextTickArgs (fn, a, b) {
|
---|
| 52 | process.nextTick(function () {
|
---|
| 53 | fn(a, b)
|
---|
| 54 | })
|
---|
| 55 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.