{"name":"thunks","description":"A small and magical async control flow tool, wrap promise, generator and anything to thunk.","authors":["Yan Qing <admin@zensh.com>"],"version":"2.7.2","main":"thunks.js","repository":{"type":"git","url":"git@github.com:thunks/thunks.git"},"homepage":"https://github.com/thunks/thunks","keywords":["async","thunk","thunks","Promise","flow","yield","generator","coro","coroutine"],"dependencies":{},"devDependencies":{"gulp":"^3.8.10","gulp-jshint":"^1.9.2","gulp-mocha":"^2.0.0","gulp-sequence":"^0.3.1","jsbench":"^0.5.1","mocha":"^2.1.0","should":"^4.6.5","thenjs":">=1.5.0","async":">=0.9.0","bluebird":">=2.9.0","co":">=4.3.0","rsvp":">=3.0.0","when":">=3.7.0"},"scripts":{"test":"node --harmony `which gulp`"},"ignore":["**/.*","benchmark","test","examples","docs","gulpfile.js","bower.json","component.json"],"gitHead":"38a8e388c20bba0acaa6b5b4e8c0f61ab03a4203","bugs":{"url":"https://github.com/thunks/thunks/issues"},"_id":"thunks@2.7.2","_shasum":"7da3c22f5ecac50be4bedd310580a17ea3168d56","_from":".","_npmVersion":"2.4.1","_nodeVersion":"1.1.0","_npmUser":{"name":"zensh","email":"admin@zensh.com"},"maintainers":[{"name":"zensh","email":"admin@zensh.com"}],"dist":{"shasum":"7da3c22f5ecac50be4bedd310580a17ea3168d56","tarball":"http://registry-npm.ywwl.com/thunks/download/thunks-2.7.2.tgz","integrity":"sha512-pNlDFaBeQ1UzSYphIyJXpisQ+hxjJoz3VvQY5SjSMJqumRBV93oXoz+I7IGcy95zYbf0+WBA9kf5Q9QCoSZ2lQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC9dOt6LZe8HSk/OD6XsLATHZnSAuSHihS346mXNe2i5QIhAOC0gqcsHSske/TRjmK7oGtDkG4mHNUlbIBi/lliOsWG"}],"size":27011,"noattachment":false,"key":"/thunks/-/thunks-2.7.2.tgz"},"directories":{},"readme":"# thunks\r\n\r\nA small and magical composer for all JavaScript asynchronous.\r\n\r\n[![NPM version][npm-image]][npm-url]\r\n[![Build Status][travis-image]][travis-url]\r\n[![js-standard-style][js-standard-image]][js-standard-url]\r\n[![Coverage Status][coveralls-image]][coveralls-url]\r\n[![Downloads][downloads-image]][downloads-url]\r\n\r\n[中文说明](https://github.com/thunks/thunks/blob/master/docs/api-zh.md)\r\n\r\n[thunks 的作用域和异常处理设计](https://github.com/thunks/thunks/blob/master/docs/scope-and-error-catch.md)\r\n\r\n## Compatibility\r\n\r\nES5+, support node.js and browsers.\r\n\r\n## Summary\r\n\r\n- [thunks](#thunks)\r\n  - [Compatibility](#compatibility)\r\n  - [Summary](#summary)\r\n  - [Implementations](#implementations)\r\n  - [What is a thunk](#what-is-a-thunk)\r\n  - [Demo](#demo)\r\n    - [with thunk function](#with-thunk-function)\r\n    - [with async function](#with-async-function)\r\n    - [with generator function](#with-generator-function)\r\n    - [chain, sequential, parallel](#chain-sequential-parallel)\r\n  - [Installation](#installation)\r\n  - [API](#api)\r\n    - [thunks([scope])](#thunksscope)\r\n    - [thunks.pruneErrorStack](#thunkspruneerrorstack)\r\n    - [thunks.onerror(error)](#thunksonerrorerror)\r\n    - [Class thunks.Scope](#class-thunksscope)\r\n    - [thunk(thunkable)](#thunkthunkable)\r\n    - [thunk.all(obj)](#thunkallobj)\r\n    - [thunk.all(thunkable1, ..., thunkableN)](#thunkallthunkable1--thunkablen)\r\n    - [thunk.seq([thunkable1, ..., thunkableN])](#thunkseqthunkable1--thunkablen)\r\n    - [thunk.seq(thunkable1, ..., thunkableN)](#thunkseqthunkable1--thunkablen)\r\n    - [thunk.race([thunkable1, ..., thunkableN])](#thunkracethunkable1--thunkablen)\r\n    - [thunk.race(thunkable1, ..., thunkableN)](#thunkracethunkable1--thunkablen)\r\n    - [thunk.thunkify(fn)](#thunkthunkifyfn)\r\n    - [thunk.lift(fn)](#thunkliftfn)\r\n    - [thunk.promise(thunkable)](#thunkpromisethunkable)\r\n    - [thunk.persist(thunkable)](#thunkpersistthunkable)\r\n    - [thunk.delay(delay)](#thunkdelaydelay)\r\n    - [thunk.stop([message])](#thunkstopmessage)\r\n    - [thunk.cancel()](#thunkcancel)\r\n  - [TypeScript Typings](#typescript-typings)\r\n  - [What functions are thunkable](#what-functions-are-thunkable)\r\n  - [License](#license)\r\n\r\n## Implementations\r\n\r\n- [Toa](https://github.com/toajs/toa) A powerful web framework rely on thunks.\r\n- [T-man](https://github.com/thunks/tman) Super test manager for JavaScript.\r\n- [thunk-redis](https://github.com/thunks/thunk-redis) The fastest thunk/promise-based redis client, support all redis features.\r\n- [thunk-disque](https://github.com/thunks/thunk-disque) A thunk/promise-based disque client.\r\n- [thunk-stream](https://github.com/thunks/thunk-stream) Wrap a readable/writable/duplex/transform stream to a thunk.\r\n- [thunk-queue](https://github.com/thunks/thunk-queue) A thunk queue for uncertainty tasks evaluation.\r\n- [thunk-loop](https://github.com/thunks/thunk-loop) Asynchronous tasks loop (while (true) { ... }).\r\n- [thunk-mocha](https://github.com/thunks/thunk-mocha) Enable support for generators in Mocha with backward compatibility.\r\n- [thunk-ratelimiter](https://github.com/thunks/thunk-ratelimiter) The fastest abstract rate limiter.\r\n- [thunk-workers](https://github.com/thunks/thunk-workers) Thunk-based task scheduler that executes synchrounous and/or asynchronous tasks under concurrency control.\r\n- [file-cache](https://github.com/thunks/file-cache) Read file with caching, rely on thunks.\r\n\r\nAnd a mountain of applications in server-side or client-side.\r\n\r\n## What is a thunk\r\n\r\n1. [ALGOL thunks in 1961](https://web.archive.org/web/20190415165418/https://portalparts.acm.org/1070000/1064045/fm/frontmatter.pdf?ip=98.14.66.142)\r\n\r\n1. **`thunk`** is a function that encapsulates synchronous or asynchronous code inside.\r\n\r\n1. **`thunk`** accepts only one `callback` function as an arguments, which is a CPS function.\r\n\r\n1. **`thunk`** returns another **`thunk`** function after being called, for chaining operations.\r\n\r\n1. **`thunk`** passes the results into a `callback` function after being excuted.\r\n\r\n1. If the return value of `callback` is a **`thunk`** function, then it will be executed first and its result will be sent to another **`thunk`** for excution, or it will be sent to another new **`thunk`** function as the value of the computation.\r\n\r\n## Demo\r\n\r\n### with thunk function\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\nconst fs = require('fs')\r\n\r\nthunk(function (done) {\r\n  fs.stat('package.json', done)\r\n})(function (error, res) {\r\n  console.log(error, res)\r\n})\r\n```\r\n\r\n### with async function\r\n\r\n```js\r\nthunk(async function () {\r\n  console.log(await Promise.resolve('await promise in an async function'))\r\n\r\n  try {\r\n    await new Promise((resolve, reject) => {\r\n      setTimeout(() => reject('catch promise error in async function'), 1000)\r\n    })\r\n  } catch (err) {\r\n    console.log(err)\r\n  }\r\n})()\r\n```\r\n\r\n### with generator function\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\nconst fs = require('fs')\r\nconst size = thunk.thunkify(fs.stat)\r\n\r\n// generator\r\nthunk(function * () {\r\n  // yield thunk function\r\n  console.log(yield size('thunks.js'))\r\n  console.log(yield size('package.json'))\r\n\r\n  // yield async function\r\n  console.log(yield async () => 'yield an async function in generator function')\r\n\r\n  // yield generator function\r\n  console.log(yield function * () { return 'yield an async function in generator function' })\r\n\r\n    // parallel run\r\n  console.log(yield thunk.all([\r\n    size('thunks.js'),\r\n    size('package.json')\r\n  ]))\r\n})()\r\n```\r\n\r\n### chain, sequential, parallel\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\nconst fs = require('fs')\r\nconst size = thunk.thunkify(fs.stat)\r\n\r\n// sequential\r\nsize('.gitignore')(function (error, res) {\r\n  console.log(error, res)\r\n  return size('thunks.js')\r\n\r\n})(function (error, res) {\r\n  console.log(error, res)\r\n  return size('package.json')\r\n\r\n})(function (error, res) {\r\n  console.log(error, res)\r\n})\r\n\r\n// sequential\r\nthunk.seq([\r\n  size('.gitignore'),\r\n  size('thunks.js'),\r\n  size('package.json')\r\n])(function (error, res) {\r\n  console.log(error, res)\r\n})\r\n\r\n// parallel\r\nthunk.all([\r\n  size('.gitignore'),\r\n  size('thunks.js'),\r\n  size('package.json')\r\n])(function (error, res) {\r\n  console.log(error, res)\r\n})\r\n```\r\n\r\n## Installation\r\n\r\n**Node.js:**\r\n\r\n    npm install thunks\r\n\r\n**Bower:**\r\n\r\n    bower install thunks\r\n\r\n**browser:**\r\n\r\n```html\r\n<script src=\"/pathTo/thunks.js\"></script>\r\n```\r\n\r\n## API\r\n\r\n```js\r\nconst thunks = require('thunks')\r\n```\r\n\r\n```js\r\nconst { thunks, thunk, slice, Scope, isAsyncFn, isGeneratorFn, isThunkableFn } = from 'thunks'\r\n```\r\n\r\n### thunks([scope])\r\n\r\nMatrix of `thunk`, it generates a `thunkFunction` factory (named `thunk`) with it's scope.\r\n\"scope\" refers to the running evironments `thunk` generated(directly or indirectly) for all child thunk functions.\r\n\r\n1. Here's how you create a basic `thunk`, any exceptions would be passed the next child thunk function:\r\n\r\n  ```js\r\n  const thunk = thunks()\r\n  ```\r\n\r\n1. Here's the way to create a `thunk` listening to all exceptions in current scope with `onerror`, and it will make sure the exceptions are not being passed to the followed child thunk function, unless `onerror` function returns `true`.\r\n\r\n  ```js\r\n  const thunk = thunks(function (error) { console.error(error) })\r\n  ```\r\n\r\n  **Equals:**\r\n  ```js\r\n  const scope = new thunks.Scope(function (error) { console.error(error) })\r\n  const thunk = thunks(scope)\r\n  ```\r\n\r\n1. Create a `thunk` with `onerror`, `onstop` and `debug` listeners. Results of this `thunk` would be passed to `debug` function first before passing to the followed child thunk function.\r\n\r\n  ```js\r\n  const thunk = thunks({\r\n    onstop: function (sig) { console.log(sig) },\r\n    onerror: function (error) { console.error(error) },\r\n    debug: function () { console.log.apply(console, arguments) }\r\n  })\r\n  ```\r\n\r\n  **Equals:**\r\n  ```js\r\n  const scope = new thunks.Scope({\r\n    onstop: function (sig) { console.log(sig) },\r\n    onerror: function (error) { console.error(error) },\r\n    debug: function () { console.log.apply(console, arguments) }\r\n  })\r\n  const thunk = thunks(scope)\r\n  ```\r\nThe context of `onerror`, `onstop` and `debug` is a `scope`.\r\nEven multiple `thunk` main functions with different scopes are composed,\r\neach scope would be separate from each other,\r\nwhich means, `onerror`, `onstop` and `debug` would not run in other scopes.\r\n\r\n### thunks.pruneErrorStack\r\n\r\nDefault to `true`, means it will prune error stack message.\r\n\r\n### thunks.onerror(error)\r\n\r\nDefault to `null`, it is a global error handler.\r\n\r\n### Class thunks.Scope\r\n\r\n```js\r\nconst scope = new thunks.Scope({\r\n  onstop: function (sig) { assert.strictEqual(this, scope) },\r\n  onerror: function (error) { assert.strictEqual(this, scope) },\r\n  debug: function () { assert.strictEqual(this, scope) }\r\n})\r\nconst thunk = thunks(scope)\r\n```\r\n\r\n### thunk(thunkable)\r\n\r\nThis is the `thunkFunction` factory, to create new `thunkFunction` functions.\r\n\r\nThe parameter `thunkable` value could be:\r\n\r\n1. a `thunkFunction` function, by calling this function a new `thunkFunction` function will be returned\r\n\r\n  ```js\r\n  let thunk1 = thunk(1)\r\n  thunk(thunk1)(function (error, value) {\r\n    console.log(error, value) // null 1\r\n  })\r\n  ```\r\n\r\n1. a thunkLike function `function (callback) {}`, when called, passes its results to the next `thunkFunction` function\r\n\r\n  ```js\r\n  thunk(function (callback) {\r\n    callback(null, 1)\r\n  })(function (error, value) {\r\n    console.log(error, value) // null 1\r\n  })\r\n  ```\r\n\r\n1. a Promise object, results of Promise would be passed to a new `thunkFunction` function\r\n\r\n  ```js\r\n  let promise = Promise.resolve(1)\r\n\r\n  thunk(promise)(function (error, value) {\r\n    console.log(error, value) // null 1\r\n  })\r\n  ```\r\n\r\n1. objects which implements the method `toThunk`\r\n\r\n  ```js\r\n  let obj = {\r\n    toThunk: function () {\r\n      return function (done) { done(null, 1) }\r\n    }\r\n  }\r\n  // `obj` has `toThunk` method that returns a thunk function\r\n  thunk(obj)(function (error, value) {\r\n    console.log(error, value) // null 1\r\n  })\r\n  ```\r\n\r\n1. objects which implement the method `toPromise`\r\n\r\n  ```js\r\n  const Rx = require('rxjs')\r\n  // Observable instance has `toPromise` method that returns a promise\r\n  thunk(Rx.Observable.fromPromise(Promise.resolve(123)))(function (error, value) {\r\n    console.log(error, value) // null 123\r\n  })\r\n  ```\r\n\r\n1. Generator and Generator Function, like `co`, but `yield` anything\r\n\r\n  ```js\r\n  thunk(function * () {\r\n    var x = yield 10\r\n    return 2 * x\r\n  })(function * (error, res) {\r\n    console.log(error, res) // null, 20\r\n\r\n    return yield thunk.all([1, 2, thunk(3)])\r\n  })(function * (error, res) {\r\n    console.log(error, res) // null, [1, 2, 3]\r\n    return yield thunk.all({\r\n      name: 'test',\r\n      value: thunk(1)\r\n    })\r\n  })(function (error, res) {\r\n    console.log(error, res) // null, {name: 'test', value: 1}\r\n  })\r\n  ```\r\n\r\n1. async/await function\r\n\r\n  ```js\r\n  thunk(async function () {\r\n    console.log(await Promise.resolve('await promise in an async function'))\r\n\r\n    try {\r\n      await new Promise((resolve, reject) => {\r\n        setTimeout(() => reject('catch promise error in async function'), 1000)\r\n      })\r\n    } catch (err) {\r\n      console.log(err)\r\n    }\r\n  })(function * () {\r\n    console.log(yield async () => 'yield an async function in generator function')\r\n  })()\r\n  ```\r\n\r\n1. values in other types that would be valid results to pass to a new child thunk function\r\n\r\n  ```js\r\n  thunk(1)(function (error, value) {\r\n    console.log(error, value) // null 1\r\n  })\r\n\r\n  thunk([1, 2, 3])(function (error, value) {\r\n    console.log(error, value) // null [1, 2, 3]\r\n  })\r\n  ```\r\n\r\nYou can also run with `this`:\r\n\r\n  ```js\r\n  thunk.call({x: 123}, 456)(function (error, value) {\r\n    console.log(error, this.x, value) // null 123 456\r\n    return 'thunk!'\r\n  })(function (error, value) {\r\n    console.log(error, this.x, value) // null 123 'thunk!'\r\n  })\r\n  ```\r\n\r\n### thunk.all(obj)\r\n\r\n### thunk.all(thunkable1, ..., thunkableN)\r\n\r\nReturns a child thunk function.\r\n\r\n`obj` can be an array or an object that contains any value. `thunk.all` will transform value to a child thunk function and excute it in parallel. After all of them are finished, an array containing results(in its original order) would be passed to the a new child thunk function.\r\n\r\n```js\r\nthunk.all([\r\n  thunk(0),\r\n  function * () { return yield 1 },\r\n  2,\r\n  thunk(function (callback) { callback(null, [3]) })\r\n])(function (error, value) {\r\n  console.log(error, value) // null [0, 1, 2, [3]]\r\n})\r\n\r\nthunk.all({\r\n  a: thunk(0),\r\n  b: thunk(1),\r\n  c: 2,\r\n  d: thunk(function (callback) { callback(null, [3]) })\r\n})(function (error, value) {\r\n  console.log(error, value) // null {a: 0, b: 1, c: 2, d: [3]}\r\n})\r\n```\r\n\r\nYou may also write code like this:\r\n\r\n```js\r\nthunk.all.call({x: [1, 2, 3]}, [4, 5, 6])(function (error, value) {\r\n  console.log(error, this.x, value) // null [1, 2, 3] [4, 5, 6]\r\n  return 'thunk!'\r\n})(function (error, value) {\r\n  console.log(error, this.x, value) // null [1, 2, 3] 'thunk!'\r\n})\r\n```\r\n\r\n### thunk.seq([thunkable1, ..., thunkableN])\r\n\r\n### thunk.seq(thunkable1, ..., thunkableN)\r\n\r\nReturns a child thunk function.\r\n\r\n`thunkX` can be any value, `thunk.seq` will transform value to a child thunk function and excute it in order. After all of them are finished, an array containing results(in its original order) would be passed to the a new child thunk function.\r\n\r\n```js\r\nthunk.seq([\r\n  function (callback) {\r\n    setTimeout(function () {\r\n      callback(null, 'a', 'b')\r\n    }, 100)\r\n  },\r\n  thunk(function (callback) {\r\n    callback(null, 'c')\r\n  }),\r\n  [thunk('d'), function * () { return yield 'e' }], // thunk in array will be excuted in parallel\r\n  function (callback) {\r\n    should(flag).be.eql([true, true])\r\n    flag[2] = true\r\n    callback(null, 'f')\r\n  }\r\n])(function (error, value) {\r\n  console.log(error, value) // null [['a', 'b'], 'c', ['d', 'e'], 'f']\r\n})\r\n```\r\n\r\nor\r\n\r\n```js\r\nthunk.seq(\r\n  function (callback) {\r\n    setTimeout(function () {\r\n      callback(null, 'a', 'b')\r\n    }, 100)\r\n  },\r\n  thunk(function (callback) {\r\n    callback(null, 'c')\r\n  }),\r\n  [thunk('d'), thunk('e')], // thunk in array will be excuted in parallel\r\n  function (callback) {\r\n    should(flag).be.eql([true, true])\r\n    flag[2] = true\r\n    callback(null, 'f')\r\n  }\r\n)(function (error, value) {\r\n  console.log(error, value) // null [['a', 'b'], 'c', ['d', 'e'], 'f']\r\n})\r\n```\r\n\r\nYou may also write code like this:\r\n\r\n```js\r\nthunk.seq.call({x: [1, 2, 3]}, 4, 5, 6)(function (error, value) {\r\n  console.log(error, this.x, value) // null [1, 2, 3] [4, 5, 6]\r\n  return 'thunk!'\r\n})(function (error, value) {\r\n  console.log(error, this.x, value) // null [1, 2, 3] 'thunk!'\r\n})\r\n```\r\n\r\n### thunk.race([thunkable1, ..., thunkableN])\r\n\r\n### thunk.race(thunkable1, ..., thunkableN)\r\n\r\nReturns a child thunk function with the value or error from one first completed.\r\n\r\n### thunk.thunkify(fn)\r\n\r\nReturns a new function that would return a child thunk function\r\n\r\nTransform a `fn` function which is in Node.js style into a new function.\r\nThis new function does not accept a `callback` as an argument, but accepts child thunk functions.\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\nconst fs = require('fs')\r\nconst fsStat = thunk.thunkify(fs.stat)\r\n\r\nfsStat('thunks.js')(function (error, result) {\r\n  console.log('thunks.js: ', result)\r\n})\r\nfsStat('.gitignore')(function (error, result) {\r\n  console.log('.gitignore: ', result)\r\n})\r\n```\r\n\r\nYou may also write code with `this`:\r\n\r\n```js\r\nlet obj = {a: 8}\r\nfunction run (x, callback) {\r\n  //...\r\n  callback(null, this.a * x)\r\n}\r\n\r\nlet run = thunk.thunkify.call(obj, run)\r\n\r\nrun(1)(function (error, result) {\r\n  console.log('run 1: ', result)\r\n})\r\nrun(2)(function (error, result) {\r\n  console.log('run 2: ', result)\r\n})\r\n```\r\n\r\n### thunk.lift(fn)\r\n\r\n`lift` comes from Haskell, it transforms a synchronous function `fn` into a new async function.\r\nThis new function will accept `thunkable` arguments, evaluate them, then run as the original function `fn`. The new function returns a child thunk function.\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\n\r\nfunction calculator (a, b, c) {\r\n  return (a + b + c) * 10\r\n}\r\n\r\nconst calculatorT = thunk.lift(calculator)\r\n\r\nlet value1 = thunk(2)\r\nlet value2 = Promise.resolve(3)\r\n\r\ncalculatorT(value1, value2, 5)(function (error, result) {\r\n  console.log(result) // 100\r\n})\r\n```\r\n\r\nYou may also write code with `this`:\r\n\r\n```js\r\nconst calculatorT = thunk.lift.call(context, calculator)\r\n```\r\n\r\n### thunk.promise(thunkable)\r\n\r\nit transforms `thunkable` value to a promise.\r\n\r\n```js\r\nconst thunk = require('thunks').thunk\r\n\r\nthunk.promise(function * () {\r\n  return yield Promise.resolve('Hello')\r\n}).then(function (res) {\r\n  console.log(res)\r\n})\r\n```\r\n\r\n### thunk.persist(thunkable)\r\n\r\nit transforms `thunkable` value to a persist thunk function, which can be called more than once with the same result(like a promise). The new function returns a child thunk function.\r\n\r\n```js\r\nconst thunk = require('thunks')()\r\n\r\nlet persistThunk = thunk.persist(thunk(x))\r\n\r\npersistThunk(function (error, result) {\r\n  console.log(1, result) // x\r\n  return persistThunk(function (error, result) {\r\n    console.log(2, result) // x\r\n    return persistThunk\r\n  })\r\n})(function (error, result) {\r\n  console.log(3, result) // x\r\n})\r\n```\r\n\r\nYou may also write code with `this`:\r\n\r\n```js\r\nconst persistThunk = thunk.persist.call(context, thunkable)\r\n```\r\n\r\n### thunk.delay(delay)\r\n\r\nReturn a child thunk function, this child thunk function will be called after `delay` milliseconds.\r\n\r\n```js\r\nconsole.log('thunk.delay 500: ', Date.now())\r\nthunk.delay(500)(function () {\r\n  console.log('thunk.delay 1000: ', Date.now())\r\n  return thunk.delay(1000)\r\n})(function () {\r\n  console.log('thunk.delay end: ', Date.now())\r\n})\r\n```\r\n\r\nYou may also write code with `this`:\r\n\r\n```js\r\nconsole.log('thunk.delay start: ', Date.now())\r\nthunk.delay.call(this, 1000)(function () {\r\n  console.log('thunk.delay end: ', Date.now())\r\n})\r\n```\r\n\r\n### thunk.stop([message])\r\n\r\nThis will stop control flow process with a message similar to Promise's cancelable(not implemented yet). It will throw a stop signal object.\r\nStop signal is an object with a message and `status === 19`(POSIX signal SIGSTOP) and a special code. Stop signal can be caught by `onstop`, and aslo can be caught by `try catch`, in this case it will not trigger `onstop`.\r\n\r\n```js\r\nconst thunk = require('thunks')({\r\n  onstop: function (res) {\r\n    if (res) console.log(res.code, res.status, res) // SIGSTOP 19 { message: 'Stop now!' }\r\n  }\r\n})\r\n\r\nthunk(function (callback) {\r\n  thunk.stop('Stop now!')\r\n  console.log('It will not run!')\r\n})(function (error, value) {\r\n  console.log('It will not run!', error)\r\n})\r\n```\r\n\r\n```js\r\nthunk.delay(100)(function () {\r\n  console.log('Hello')\r\n  return thunk.delay(100)(function () {\r\n    thunk.stop('Stop now!')\r\n    console.log('It will not run!')\r\n  })\r\n})(function (error, value) {\r\n  console.log('It will not run!')\r\n})\r\n```\r\n\r\n### thunk.cancel()\r\n\r\nThis will cancel all control flow process in the current thunk's scope.\r\n\r\n## TypeScript Typings\r\n\r\n```typescript\r\nimport * as assert from 'assert'\r\nimport { thunk, thunks, isGeneratorFn } from 'thunks'\r\n// or: import * as thunks from 'thunks'\r\n\r\nthunk(function * () {\r\n  assert.strictEqual(yield thunks()(1), 1)\r\n  assert.ok(isGeneratorFn(function * () {}))\r\n\r\n  while (true) {\r\n    yield function (done) { setTimeout(done, 1000) }\r\n    console.log('Dang!')\r\n  }\r\n})()\r\n```\r\n\r\n## What functions are thunkable\r\n\r\nthunks supports so many [thunkable](#thunkthunkable) objects. There are three kind of functions:\r\n\r\n- thunk-like function `function (callback) { callback(err, someValue) }`\r\n- generator function `function * () { yield something }`\r\n- async/await function `async function () { await somePromise }`\r\n\r\nthunks can't support common functions (non-thunk-like functions). thunks uses `fn.length === 1` to recognize thunk-like functions.\r\n\r\nUsing a common function in this way will throw an error:\r\n\r\n```js\r\nthunk(function () {})(function (err) {\r\n  console.log(1, err) // 1 [Error: Not thunkable function: function () {}]\r\n})\r\n\r\nthunk(function (a, b) {})(function (err) {\r\n  console.log(2, err) // 2 [Error: Not thunkable function: function (a, b) {}]\r\n})\r\n\r\nthunk(function () { let callback = arguments[0]; callback() })(function (err) {\r\n  console.log(3, err) // 3 [Error: Not thunkable function: function () { let callback = arguments[0]; callback() }]\r\n})\r\n\r\nthunk()(function () {\r\n  return function () {} // can't return a non-thunkable function.\r\n})(function (err) {\r\n  console.log(4, err) // 4 [Error: Not thunkable function: function () {}]\r\n})\r\n```\r\n\r\nSo pay attention to that. **We can't return a non-thunkable function** in thunk. If we return a thunkable function, thunk will evaluate it as an asynchronous task.\r\n\r\n## License\r\n\r\nthunks is licensed under the [MIT](https://github.com/thunks/thunks/blob/master/LICENSE) license.\r\nCopyright &copy; 2014-2020 thunks.\r\n\r\n[npm-url]: https://npmjs.org/package/thunks\r\n[npm-image]: http://img.shields.io/npm/v/thunks.svg\r\n\r\n[travis-url]: https://travis-ci.org/thunks/thunks\r\n[travis-image]: http://img.shields.io/travis/thunks/thunks.svg\r\n\r\n[coveralls-url]: https://coveralls.io/r/thunks/thunks\r\n[coveralls-image]: https://coveralls.io/repos/thunks/thunks/badge.svg\r\n\r\n[downloads-url]: https://npmjs.org/package/thunks\r\n[downloads-image]: http://img.shields.io/npm/dm/thunks.svg?style=flat-square\r\n\r\n[js-standard-url]: https://github.com/feross/standard\r\n[js-standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat\r\n","publish_time":1423395803121,"_cnpm_publish_time":1423395803121,"dist-tags":{"latest":"4.9.6"}}