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

Commit

Permalink
improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 18, 2016
1 parent d11c7ca commit 31b0765
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 222 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
node_modules/
debug/
coverage/
typings/
test.js

npm-debug.log
Expand Down
71 changes: 63 additions & 8 deletions README.md
Expand Up @@ -8,7 +8,7 @@ A small and magical composer for all JavaScript asynchronous.
[![Coverage Status][coveralls-image]][coveralls-url]
[![Downloads][downloads-image]][downloads-url]

[中文说明](https://github.com/thunks/thunks/blob/master/README_zh.md)
[中文说明](https://github.com/thunks/thunks/blob/master/docs/api-zh.md)

[thunks 的作用域和异常处理设计](https://github.com/thunks/thunks/blob/master/docs/scope-and-error-catch.md)

Expand Down Expand Up @@ -39,6 +39,8 @@ ES5+, support node.js and browsers.
- [thunk.delay(delay)](#thunkdelaydelay)
- [thunk.stop([messagge])](#thunkstopmessagge)
- [thunk.cancel()](#thunkcancel)
- [TypesSript Typings](#typescript-typings)
- [What functions are thunkable?](#what-functions-are-thunkable)
- [License MIT](#license)

## Implementations:
Expand Down Expand Up @@ -255,10 +257,12 @@ The parameter `thunkable` value could be:

```js
let thunk1 = thunk(1)
let thunk2 = thunk(thunk1) // thunk2 equals to thunk1
thunk(thunk1)(function (error, value) {
console.log(error, value) // null 1
})
```

2. `function (callback) {}`, by calling it, results woule be gathered and be passed to the next `thunkFunction` function
2. a thunkLike function `function (callback) {}`, by calling it, results woule be gathered and be passed to the next `thunkFunction` function

```js
thunk(function (callback) {
Expand All @@ -278,20 +282,25 @@ The parameter `thunkable` value could be:
})
```

4. objects which implements methods of `toThunk`
4. objects which implements the method `toThunk`

```js
let then = Thenjs(1) // then.toThunk() return a thunk function

thunk(then)(function (error, value) {
let obj = {
toThunk: function () {
return function (done) { done(null, 1) }
}
}
// `obj` has `toThunk` method that return a thunk function
thunk(obj)(function (error, value) {
console.log(error, value) // null 1
})
```

5. objects which implements methods of `toPromise`
5. objects which implements the method `toPromise`

```js
const Rx = require('rxjs')
// Observable instance has `toPromise` method that return a promise
thunk(Rx.Observable.fromPromise(Promise.resolve(123)))(function (error, value) {
console.log(error, value) // null 123
})
Expand Down Expand Up @@ -615,6 +624,52 @@ thunk.delay(100)(function () {

This will cancel all control flow process in the current thunk's scope.

## TypesSript Typings

```typescript
import * as thunks from '../'
const thunk = thunks()

thunk(function * () {
while (true) {
yield <any>function (done) { setTimeout(done, 1000) }
console.log('Dang!')
}
})()
```

## What functions are thunkable?

thunks supports so many [thunkable](#thunkthunkable) objects. There are three kind of functions:

- thunk like function `function (callback) { callback(err, someValue) }`
- generator function `function * () { yield something }`
- async/await function `async function () { await somePromise }`

thunks can't suports common function (not thunk like function). thunks uses `fn.length === 1` to recognize thunk like function.

Use common function in this way will throw error:
```js
thunk(function () {})(function (err) {
console.log(1, err) // 1 [Error: Not thunkable function: function () {}]
})

thunk(function (a, b) {})(function (err) {
console.log(2, err) // 2 [Error: Not thunkable function: function (a, b) {}]
})

thunk(function () { let callback = arguments[0]; callback() })(function (err) {
console.log(3, err) // 3 [Error: Not thunkable function: function () { let callback = arguments[0]; callback() }]
})

thunk()(function () {
return function () {} // can't return a non-thunkable function.
})(function (err) {
console.log(4, err) // 4 [Error: Not thunkable function: function () {}]
})
```
So pay attention to that **we can't return a non-thunkable function** in thunk. If we return a thunkable function, thunk will evaluate it to get a asynchronous value.

## License
thunks is licensed under the [MIT](https://github.com/thunks/tman/blob/master/LICENSE) license.
Copyright &copy; 2016 thunks.
Expand Down
90 changes: 0 additions & 90 deletions README_zh.md → docs/api-zh.md
Expand Up @@ -9,14 +9,6 @@ A small and magical composer for all JavaScript asynchronous.
[![Downloads][downloads-image]][downloads-url]
[![Talk topic][talk-image]][talk-url]

## [Toa](https://github.com/toajs/toa): A powerful web framework rely on thunks.

[thunks 的作用域和异常处理设计](https://github.com/thunks/thunks/blob/master/docs/scope-and-error-catch.md)

## Compatibility

ES5+, support node.js and browsers.

## `thunk` 是什么?

0. [ALGOL thunks in 1961](http://archive.computerhistory.org/resources/text/algol/ACM_Algol_bulletin/1064045/frontmatter.pdf)
Expand All @@ -31,88 +23,6 @@ ES5+, support node.js and browsers.

5. `callback` 的返回值如果是 **`thunk`** 函数,则等该 **`thunk`** 执行完毕将结果输入新 **`thunk`** 函数运行;如果是其它值,则当做正确结果进入新的 **`thunk`** 函数运行;

## Demo

```js
const thunk = require('thunks')()
const fs = require('fs')
const size = thunk.thunkify(fs.stat)

// generator
thunk(function * () {

// sequential
console.log(yield size('.gitignore'))
console.log(yield size('thunks.js'))
console.log(yield size('package.json'))

})(function * (error, res) {
//parallel
console.log(yield [
size('.gitignore'),
size('thunks.js'),
size('package.json')
])
})()
```

```js
const thunk = require('thunks')()
const fs = require('fs')
const size = thunk.thunkify(fs.stat)

// sequential
size('.gitignore')(function (error, res) {
console.log(error, res)
return size('thunks.js')

})(function (error, res) {
console.log(error, res)
return size('package.json')

})(function (error, res) {
console.log(error, res)
})

// parallel
thunk.all([
size('.gitignore'),
size('thunks.js'),
size('package.json')
])(function (error, res) {
console.log(error, res)
})

// sequential
thunk.seq([
size('.gitignore'),
size('thunks.js'),
size('package.json')
])(function (error, res) {
console.log(error, res)
})
```

## Install

**Node.js:**

```sh
npm install thunks
```

**bower:**

```sh
bower install thunks
```

**Browser:**

```html
<script src="/pathTo/thunks.js"></script>
```

## API

```js
Expand Down
22 changes: 22 additions & 0 deletions examples/function-error.js
@@ -0,0 +1,22 @@
'use strict'

var thunks = require('..')
var thunk = thunks()

thunk(function () {})(function (err) {
console.log(1, err) // 1 [Error: Not thunkable function: function () {}]
})

thunk(function (a, b) {})(function (err) {
console.log(2, err) // 2 [Error: Not thunkable function: function (a, b) {}]
})

thunk(function () { let callback = arguments[0]; callback() })(function (err) {
console.log(3, err) // 3 [Error: Not thunkable function: function () { let callback = arguments[0]; callback() }]
})

thunk()(function () {
return function () {} // can't return a not thunkable function.
})(function (err) {
console.log(4, err) // 4 [Error: Not thunkable function: function () {}]
})
13 changes: 13 additions & 0 deletions examples/simple.ts
@@ -0,0 +1,13 @@
'use strict'

// `ts-node examples/simple.ts`

import * as thunks from '../'
const thunk = thunks()

thunk(function * () {
while (true) {
yield function (done) { setTimeout(done, 1000) }
console.log('Dang!')
}
})()
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -4,9 +4,9 @@
"authors": [
"Yan Qing <admin@zensh.com>"
],
"version": "4.7.1",
"version": "4.7.2",
"main": "thunks.js",
"typings": "./thunks.d.ts",
"typings": "thunks.d.ts",
"jsnext:main": "thunks.es6.js",
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,17 +34,20 @@
"bluebird": "^3.4.6",
"co": "^4.6.0",
"istanbul": "^0.4.5",
"jsbench": "^1.0.2",
"jsbench": "^1.1.0",
"promise": "^7.1.1",
"regenerator": "^0.8.46",
"should": "^11.1.0",
"standard": "^8.0.0",
"thenjs": "^2.0.3",
"tman": "^1.4.3"
"tman": "^1.4.5",
"ts-node": "^1.3.0",
"typescript": "^2.0.2"
},
"scripts": {
"test": "standard && tman test/index",
"test-cov": "istanbul cover _tman test/index",
"test-typings": "tman -r ts-node/register test/typings.test.ts",
"bench": "node benchmark/index"
},
"files": [
Expand Down
7 changes: 7 additions & 0 deletions test.ts
@@ -0,0 +1,7 @@

type mtypes = boolean | number | string | Array<any> | void;

export function testfn (): any {
let test: mtypes = [1,2,3];
console.log(test)
}

0 comments on commit 31b0765

Please sign in to comment.