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 04835a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 101 deletions.
65 changes: 13 additions & 52 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 @@ -274,7 +276,7 @@ You can also run with `this`:
```

### thunk.all(obj)
### thunk.all(thunk1, ..., thunkX)
### thunk.all(thunkable1, ..., thunkableN)

Returns a child thunk function.

Expand Down Expand Up @@ -311,8 +313,8 @@ thunk.all.call({x: [1, 2, 3]}, [4, 5, 6])(function (error, value) {
})
```

### thunk.seq([thunk1, ..., thunkX])
### thunk.seq(thunk1, ..., thunkX)
### thunk.seq([thunkable1, ..., thunkableN])
### thunk.seq(thunkable1, ..., thunkableN)

Returns a child thunk function.

Expand Down Expand Up @@ -372,48 +374,11 @@ thunk.seq.call({x: [1, 2, 3]}, 4, 5, 6)(function (error, value) {
})
```

### thunk.race([thunk1, ..., thunkX])
### thunk.race(thunk1, ..., thunkX)
### thunk.race([thunkable1, ..., thunkableN])
### thunk.race(thunkable1, ..., thunkableN)

Returns a child thunk function with the value or error from one first completed.

### thunk.digest(error, val1, val2, ...)

Returns a child thunk function.

Transform a Node.js callback function into a child thunk function.
This child thunk function retuslts in `(error, val1, val2, ...)`, which is just being passed to a new child thunk function,
like:

```js
thunk(function (callback) {
callback(error, val1, val2, ...)
})
```

One use case:

```js
thunk(function (callback) {
//...
callback(error, result)
})(function (error, value) {
//...
return thunk.digest(error, value)
})(function (error, value) {
//...
})
```

You may also write code with `this`

```js
var a = {x: 1}
thunk.digest.call(a, null, 1, 2)(function (error, value1, value2) {
console.log(this, error, value1, value2) // { x: 1 } null 1 2
})
```

### thunk.thunkify(fn)

Returns a new function that would return a child thunk function
Expand All @@ -422,7 +387,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 +424,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 +451,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 +501,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 All @@ -562,10 +527,6 @@ thunk.delay(100)(function () {
})
```

## Who's using

+ Teambition: https://www.teambition.com/ use in server-side and client-side

[npm-url]: https://npmjs.org/package/thunks
[npm-image]: http://img.shields.io/npm/v/thunks.svg

Expand Down
59 changes: 10 additions & 49 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 @@ -236,7 +236,7 @@ thunk.call({x: 123}, 456)(function (error, value) {


### thunk.all(obj)
### thunk.all(thunk1, ..., thunkX)
### thunk.all(thunkable1, ..., thunkableN)

返回一个新的子 thunk 函数。

Expand Down Expand Up @@ -275,8 +275,8 @@ thunk.all.call({x: [1, 2, 3]}, [4, 5, 6])(function (error, value) {
})
```

### thunk.seq([thunk1, ..., thunkX])
### thunk.seq(thunk1, ..., thunkX)
### thunk.seq([thunkable1, ..., thunkableN])
### thunk.seq(thunkable1, ..., thunkableN)

返回一个新的子 thunk 函数。

Expand Down Expand Up @@ -336,54 +336,19 @@ thunk.seq.call({x: [1, 2, 3]}, 4, 5, 6)(function (error, value) {
})
```

### thunk.race([thunk1, ..., thunkX])
### thunk.race(thunk1, ..., thunkX)
### thunk.race([thunkable1, ..., thunkableN])
### thunk.race(thunkable1, ..., thunkableN)

返回一个新的子 thunk 函数。迭代数组所有子 thunk 函数最先完成的运算结果会传入其中,无论正确或错误。

### thunk.digest(error, val1, val2, ...)

返回一个新的子 thunk 函数。

将 nodejs callback 风格的输入值转换成一个新的子 thunk 函数,该子 thunk 函数的结果值为 `(error, val1, val2, ...)`,即直接将 `digest` 的参数传入到新的子 thunk 函数,相当于:

```js
thunk(function (callback) {
callback(error, val1, val2, ...)
})
```

使用场景:

```js
thunk(function (callback) {
//...
callback(error, result)
})(function (error, value) {
//...
return thunk.digest(error, value)
})(function (error, value) {
//...
})
```

还可以这样运行(this):

```js
var a = {x: 1}
thunk.digest.call(a, null, 1, 2)(function (error, value1, value2) {
console.log(this, error, value1, value2) // { x: 1 } null 1 2
})
```

### thunk.thunkify(fn)

返回一个新函数,运行该函数会返回子 thunk 函数。

将带 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 +384,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 +411,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 Expand Up @@ -523,10 +488,6 @@ thunk.delay(100)(function () {
})
```

## Who's using

+ Teambition: https://www.teambition.com/ use in server-side and client-side

[npm-url]: https://npmjs.org/package/thunks
[npm-image]: http://img.shields.io/npm/v/thunks.svg

Expand Down

0 comments on commit 04835a2

Please sign in to comment.