Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jun 6, 2016
1 parent 0f448a4 commit 25429c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ And a mountain of applications in server-side or client-side.

## What is a thunk?

0. [ALGOL thunks in 1961](http://archive.computerhistory.org/resources/text/algol/ACM_Algol_bulletin/1064045/frontmatter.pdf)

1. **`thunk`** is a function that encapsulates synchronous or asynchronous code inside.

2. **`thunk`** accepts only one `callback` function as an arguments, which is a CPS function.
Expand All @@ -70,7 +72,7 @@ or it will be sent to another new **`thunk`** function as the value of the compu
## Demo

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')

var size = thunk.thunkify(fs.stat)
Expand All @@ -94,7 +96,7 @@ thunk(function *() {
```

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')

var size = thunk.thunkify(fs.stat)
Expand Down Expand Up @@ -422,7 +424,7 @@ Transform a `fn` function which is in Node.js style into a new function.
This new function does not accept `callback` as arguments, but accepts child thunk functions.

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')
var fsStat = thunk.thunkify(fs.stat)

Expand Down Expand Up @@ -459,7 +461,7 @@ run(2)(function (error, result) {
This new function will accept `thunkable` arguments, evaluate them, then run as the original function `fn`. The new function return a child thunk function.

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()

function calculator (a, b, c) {
return (a + b + c) * 10
Expand All @@ -486,7 +488,7 @@ var calculatorT = thunk.lift.call(context, calculator)
it transform `thunkable` value to a persist thunk function, which can be called more than once with the same result(like as promise). The new function return a child thunk function.

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()

var persistThunk = thunk.persist(thunk(x))

Expand Down Expand Up @@ -536,7 +538,7 @@ This will stop control flow process with a message similar to Promise's cancelab
Stop signal is a 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`.

```js
var thunk = require('../thunks.js')({
var thunk = require('thunks')({
onstop: function (res) {
if (res) console.log(res.code, res.status, res) // SIGSTOP 19 { message: 'Stop now!' }
}
Expand Down
10 changes: 5 additions & 5 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ES3+, support node.js and all browsers.
## Demo

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')

var size = thunk.thunkify(fs.stat)
Expand All @@ -56,7 +56,7 @@ thunk(function *() {
```

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')

var size = thunk.thunkify(fs.stat)
Expand Down Expand Up @@ -383,7 +383,7 @@ thunk.digest.call(a, null, 1, 2)(function (error, value1, value2) {
将带 callback 参数的 nodejs 风格的函数 `fn` 转换成一个新的函数,新函数不再接收 `callback`,其输出为子 thunk 函数。

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()
var fs = require('fs')
var fsStat = thunk.thunkify(fs.stat)

Expand Down Expand Up @@ -419,7 +419,7 @@ run(2)(function (error, result) {
`lift` 概念来自于 Haskell,它将一个同步函数转化成一个异步函数。该异步函数接受 `thunkable` 参数,等所有参数求得真实值后,再按照原函数逻辑运行。该异步函数返回子 thunk 函数。

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()

function calculator (a, b, c) {
return (a + b + c) * 10
Expand All @@ -446,7 +446,7 @@ var calculatorT = thunk.lift.call(context, calculator)
`thunkable` 值转换成一个可以持久化的 thunk 函数,可以无限次运行该函数而取得其值。

```js
var thunk = require('../thunks.js')()
var thunk = require('thunks')()

var persistThunk = thunk.persist(thunk(x))

Expand Down

0 comments on commit 25429c5

Please sign in to comment.