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

Commit

Permalink
support obj that has "toPromise" method, such as Rx. Observable insta…
Browse files Browse the repository at this point in the history
…nce.
  • Loading branch information
zensh committed Aug 3, 2016
1 parent 1e8b679 commit e82acc2
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 44 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var thunks = require('thunks')

### thunks([scope])

Matrix of `thunk`, it generates a `thunk` generator function with it's scope.
Matrix of `thunk`, it generates a `thunkFunction` factory (named `thunk`) with it's scope.
"scope" refers to the running evironments `thunk` generated(directly or indirectly) for all child thunk functions.

1. Here's how you create a basic `thunk`, any exceptions would be passed the next child thunk function:
Expand Down Expand Up @@ -253,18 +253,18 @@ var thunk = thunks(scope)

### thunk(thunkable)

This is the main function, named `thunk generator`, to create new child thunk functions.
This is the `thunkFunction` factory, to create new `thunkFunction` functions.

The parameter `thunkable` value could be:

1. a child thunk function, by calling this function a new child thunk function will be returned
1. a `thunkFunction` function, by calling this function a new `thunkFunction` function will be returned

```js
var thunk1 = thunk(1)
var thunk2 = thunk(thunk1) // thunk2 equals to thunk1
```

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

```js
thunk(function (callback) {
Expand All @@ -274,7 +274,7 @@ The parameter `thunkable` value could be:
})
```

3. a Promise object, results of Promise would be passed to a new child thunk function
3. a Promise object, results of Promise would be passed to a new `thunkFunction` function

```js
var promise = Promise.resolve(1)
Expand All @@ -294,7 +294,16 @@ The parameter `thunkable` value could be:
})
```

5. Generator and Generator Function, like `co`, but `yield` anything
5. objects which implements methods of `toPromise`

```js
var Rx = require('rxjs')
thunk(Rx.Observable.fromPromise(Promise.resolve(123)))(function (error, value) {
console.log(error, value) // null 123
})
```

6. Generator and Generator Function, like `co`, but `yield` anything

```js
thunk(function *() {
Expand All @@ -315,7 +324,7 @@ The parameter `thunkable` value could be:
})
```

6. async/await function
7. async/await function

```js
thunk(async function () {
Expand All @@ -333,7 +342,7 @@ The parameter `thunkable` value could be:
})()
```

7. values in other types would be valid results passing to a new child thunk function
8. values in other types would be valid results passing to a new child thunk function

```js
thunk(1)(function (error, value) {
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "thunks",
"description": "A small and magical composer for all JavaScript asynchronous.",
"main": "thunks.js",
"version": "4.4.2",
"version": "4.4.3",
"homepage": "https://github.com/thunks/thunks",
"authors": [
"Yan Qing <admin@zensh.com>"
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "thunks",
"description": "A small and magical composer for all JavaScript asynchronous.",
"main": "thunks.js",
"version": "4.4.2",
"version": "4.4.3",
"homepage": "https://github.com/thunks/thunks",
"authors": [
"Yan Qing <admin@zensh.com>"
Expand Down
17 changes: 17 additions & 0 deletions examples/rxjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'
/*global module, process*/

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

thunk(function * () {
console.log('1. yield Rx.Observable', yield Rx.Observable.fromPromise(Promise.resolve(123)))
console.log('2. yield Rx.Observable', yield Rx.Observable.bindNodeCallback(fs.stat)('thunks.js'))

try {
yield Rx.Observable.fromPromise(Promise.reject(new Error('some error')))
} catch (err) {
console.log('3. catch Rx.Observable error', err)
}
})()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <admin@zensh.com>"
],
"version": "4.4.2",
"version": "4.4.3",
"main": "thunks.js",
"typings": "./thunks.d.ts",
"jsnext:main": "thunks.es6.js",
Expand Down Expand Up @@ -35,6 +35,7 @@
"co": "^4.6.0",
"istanbul": "^0.4.4",
"jsbench": "^1.0.1",
"promise": "^7.1.1",
"regenerator": "^0.8.46",
"should": "^10.0.0",
"standard": "^7.1.2",
Expand Down
76 changes: 47 additions & 29 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var tman = require('tman')
var util = require('util')
var should = require('should')
var thenjs = require('thenjs')
var Promise = global.Promise || require('promise')
var thunks = require('..')
var x = {}
var supportGeneratorFn = false
Expand Down Expand Up @@ -418,35 +419,31 @@ tman.suite('thunks', function () {

tman.it('thunk(promise)', function (done) {
var thunk = thunks()
if (typeof Promise === 'function') {
thunk(Promise.resolve(x))(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
return thunk(Promise.reject(new Error('some error!')))
})(function (error, value) {
should(error).be.instanceOf(Error)
should(error.message).be.equal('some error!')
should(value).be.equal(undefined)
return new Promise(function (resolve, reject) {
setImmediate(function () {
resolve(x)
})
thunk(Promise.resolve(x))(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
return thunk(Promise.reject(new Error('some error!')))
})(function (error, value) {
should(error).be.instanceOf(Error)
should(error.message).be.equal('some error!')
should(value).be.equal(undefined)
return new Promise(function (resolve, reject) {
setImmediate(function () {
resolve(x)
})
})(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
return Promise.reject(null)
})(function (error, value) {
should(error).be.instanceof(Error)
should(value).be.equal(undefined)
return Promise.reject()
})(function (error, value) {
should(error).be.instanceof(Error)
should(value).be.equal(undefined)
})(done)
} else {
done()
}
})
})(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
return Promise.reject(null)
})(function (error, value) {
should(error).be.instanceof(Error)
should(value).be.equal(undefined)
return Promise.reject()
})(function (error, value) {
should(error).be.instanceof(Error)
should(value).be.equal(undefined)
})(done)
})

tman.it('thunk(toThunk)', function (done) {
Expand All @@ -472,6 +469,27 @@ tman.suite('thunks', function () {
})(done)
})

tman.it('thunk(toPromise)', function (done) {
var thunk = thunks()
thunk(Promise.resolve(x))(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
return Promise.reject(new Error('some error!'))
})(function (error, value) {
should(error).be.instanceOf(Error)
should(error.message).be.equal('some error!')
should(value).be.equal(undefined)
return new Promise(function (resolve, reject) {
setImmediate(function () {
resolve(x)
})
})
})(function (error, value) {
should(error).be.equal(null)
should(value).be.equal(x)
})(done)
})

tman.it('thunk.call()', function (done) {
var thunk = thunks()
thunk.call(x, 1)(function (error, value) {
Expand Down Expand Up @@ -585,7 +603,7 @@ tman.suite('thunks', function () {
return thunk.all({
a: 1,
b: thunk(2),
c: typeof Promise === 'function' ? Promise.resolve(3) : 3,
c: Promise.resolve(3),
d: thunk(function (callback) {
setImmediate(function () {
callback(null, 4)
Expand Down
9 changes: 8 additions & 1 deletion thunks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ interface IThunk {
(thunkable: IToThunkFn): Ithunk
}

interface IThunk {
(thunkable: IToPromiseFn): Ithunk
}

interface IThunk {
(thunkable: IIterator<any>): Ithunk
}
Expand All @@ -61,10 +65,13 @@ interface IThunkFn {
}

interface IToThunkFn {
(...params: any[]): any
toThunk:() => Ithunk
}

interface IToPromiseFn {
toPromise:() => IPromise<any>
}

interface IThunksOptions {
onstop: (sig: ISigStop) => any,
onerror: (error: Error) => any,
Expand Down
3 changes: 2 additions & 1 deletion thunks.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ SigStop.prototype.code = 'SIGSTOP'

function childThunk (parent, domain) {
parent.next = new Link(null, null)
return function (callback) {
return function thunkFunction (callback) {
return child(parent, domain, callback)
}
}
Expand Down Expand Up @@ -215,6 +215,7 @@ function toThunk (obj, thunkObj) {
if (isGenerator(obj)) return generatorToThunk(obj)
if (isFunction(obj.toThunk)) return obj.toThunk()
if (isFunction(obj.then)) return promiseToThunk(obj)
if (isFunction(obj.toPromise)) return promiseToThunk(obj.toPromise())
if (thunkObj && (Array.isArray(obj) || isObject(obj))) return objectToThunk(obj, thunkObj)
return obj
}
Expand Down
5 changes: 3 additions & 2 deletions thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@

function childThunk (parent, domain) {
parent.next = new Link(null, null)
return function (callback) {
return function thunkFunction (callback) {
return child(parent, domain, callback)
}
}
Expand Down Expand Up @@ -251,6 +251,7 @@
if (isGenerator(obj)) return generatorToThunk(obj)
if (isFunction(obj.toThunk)) return obj.toThunk()
if (isFunction(obj.then)) return promiseToThunk(obj)
if (isFunction(obj.toPromise)) return promiseToThunk(obj.toPromise())
if (thunkObj && (isArray(obj) || isObject(obj))) return objectToThunk(obj, thunkObj)
return obj
}
Expand Down Expand Up @@ -406,7 +407,7 @@
}

thunks.NAME = 'thunks'
thunks.VERSION = '4.4.2'
thunks.VERSION = '4.4.3'
thunks.strictMode = true
thunks['default'] = thunks
thunks.pruneErrorStack = true
Expand Down

0 comments on commit e82acc2

Please sign in to comment.