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

Commit

Permalink
add thunk.promise function.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Sep 22, 2017
1 parent a38749a commit 47e5833
Show file tree
Hide file tree
Showing 34 changed files with 477 additions and 466 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,7 @@ ES5+, support node.js and browsers.
- [thunk.race(thunkable1, ..., thunkableN)](#thunkracethunkable1--thunkablen-1)
- [thunk.thunkify(fn)](#thunkthunkifyfn)
- [thunk.lift(fn)](#thunkliftfn)
- [thunk.promise(thunkable)](#thunkpromisethunkable)
- [thunk.persist(thunkable)](#thunkpersistthunkable)
- [thunk.delay(delay)](#thunkdelaydelay)
- [thunk.stop([messagge])](#thunkstopmessagge)
Expand Down Expand Up @@ -559,6 +560,20 @@ You may also write code with `this`:
const calculatorT = thunk.lift.call(context, calculator)
```

### thunk.promise(thunkable)

it transforms `thunkable` value to a promise.

```js
const thunk = require('thunks').thunk

thunk.promise(function * () {
return yield Promise.resolve('Hello')
}).then(function (res) {
console.log(res)
})
```

### thunk.persist(thunkable)

it 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.
Expand Down
2 changes: 1 addition & 1 deletion benchmark/thunks-gen.js
@@ -1,6 +1,6 @@
'use strict'

const thunk = require('../thunks.js')()
const thunk = require('..').thunk

module.exports = function (len, syncMode) {
var task
Expand Down
2 changes: 1 addition & 1 deletion benchmark/thunks.js
@@ -1,6 +1,6 @@
'use strict'

const thunk = require('../thunks.js')()
const thunk = require('..').thunk

module.exports = function (len, syncMode) {
var task
Expand Down
2 changes: 1 addition & 1 deletion examples/async_await.es → examples/async_await.mjs
@@ -1,7 +1,7 @@
'use strict'

// Try it in Chrome 54.0.2813.0 canary (64-bit) with `enable-javascript-harmony`
const thunk = thunks()
const thunk = require('..').thunk

thunk.call({test: 'Test a async function in thunks'}, async function () {
console.log(111, `${this.test}, start:`)
Expand Down
2 changes: 1 addition & 1 deletion examples/extreme.js
@@ -1,6 +1,6 @@
'use strict'

var thunk = require('../thunks.js')()
var thunk = require('..').thunk
var thunkFn = thunk(0)

function callback (error, value) {
Expand Down
2 changes: 1 addition & 1 deletion examples/generator-array1.js
@@ -1,6 +1,6 @@
'use strict'

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

var size = thunk.thunkify(fs.stat)
Expand Down
2 changes: 1 addition & 1 deletion examples/generator-array2.js
@@ -1,6 +1,6 @@
'use strict'

var thunk = require('../thunks.js')()
var thunk = require('..').thunk
var request = require('request')
var get = thunk.thunkify.call(request, request.get)

Expand Down
2 changes: 1 addition & 1 deletion examples/generator-catch-error.js
@@ -1,7 +1,7 @@
'use strict'
/* global noneFn */

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

thunk(function * () {
// catch error by yourself
Expand Down
2 changes: 1 addition & 1 deletion examples/generator-nested.js
@@ -1,6 +1,6 @@
'use strict'

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

var size = thunk.thunkify(fs.stat)
Expand Down
2 changes: 1 addition & 1 deletion examples/generator-requests.js
@@ -1,6 +1,6 @@
'use strict'

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

var get = thunk.thunkify(request)
Expand Down
2 changes: 1 addition & 1 deletion examples/generator-return.js
@@ -1,6 +1,6 @@
'use strict'

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

var read = thunk.thunkify(fs.readFile)
Expand Down
2 changes: 1 addition & 1 deletion examples/node-flow.js
@@ -1,6 +1,6 @@
'use strict'

var thunks = require('../thunks.js')
var thunks = require('..').thunk
var fs = require('fs')
var thunk = thunks(function (error) { console.error('Thunk error:', error) })

Expand Down
2 changes: 1 addition & 1 deletion examples/observable.js
Expand Up @@ -2,7 +2,7 @@

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

thunk(function * () {
console.log('1. yield Rx.Observable', yield Rx.Observable.fromPromise(Promise.resolve(123)))
Expand Down
2 changes: 1 addition & 1 deletion examples/parallel1.js
@@ -1,6 +1,6 @@
'use strict'

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

console.time('thunk_parallel')
thunk.all([
Expand Down
2 changes: 1 addition & 1 deletion examples/parallel2.js
@@ -1,6 +1,6 @@
'use strict'

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

console.time('thunk_parallel')
thunk.all([1, 2, 3, 4, 5].map(function (index) {
Expand Down
2 changes: 1 addition & 1 deletion examples/series1.js
@@ -1,6 +1,6 @@
'use strict'

var thunk = require('../thunks.js')()
var thunk = require('..').thunk
var result = []
var thunkFn = thunk(1)

Expand Down
2 changes: 1 addition & 1 deletion examples/series2.js
@@ -1,6 +1,6 @@
'use strict'

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

console.time('Thunk_series')
thunk.seq([1, 2, 3, 4, 5].map(function (index) {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.js
@@ -1,6 +1,6 @@
'use strict'

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

var size = thunk.thunkify(fs.stat)
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.ts
Expand Up @@ -3,7 +3,7 @@
// `ts-node examples/simple.ts`

import * as assert from 'assert'
import { thunk, thunks, isGeneratorFn } from '../'
import { thunk, thunks, isGeneratorFn } from '..'
// or: import * as thunks from 'thunks'

thunk(function * () {
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-catch-error1.js
@@ -1,7 +1,7 @@
'use strict'
/* global noneFn */

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

thunk(function (callback) {
noneFn()
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-catch-error2.js
@@ -1,7 +1,7 @@
'use strict'
/* global noneFn */

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

thunk(function (callback) {
noneFn()
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-catch-error3.js
@@ -1,7 +1,7 @@
'use strict'
/* global noneFn */

var thunk = require('../thunks.js')(function (error) {
var thunk = require('..')(function (error) {
// any error will be catched by this function
console.log('catched error: ', error)
// catched error: [ReferenceError: noneFn is not defined]
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-catch-error4.js
@@ -1,7 +1,7 @@
'use strict'
/* global noneFn */

var thunk = require('../thunks.js')(function (error) {
var thunk = require('..')(function (error) {
// any error will be catched by this function
console.log('catched error: ', error)
// catched error: [ReferenceError: noneFn is not defined]
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-co.js
@@ -1,6 +1,6 @@
'use strict'

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

var thunkFn = thunk(function (callback) {
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-context.js
@@ -1,6 +1,6 @@
'use strict'

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

thunk.call({x: 123}, 456)(function (error, value) {
console.log(error, this.x, value) // null 123 456
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-stop.js
@@ -1,6 +1,6 @@
'use strict'

var thunk = require('../thunks.js')({
var thunk = require('..')({
onstop: function (res) {
if (res) console.log(res.code, res.status, res) // SIGSTOP 19 { message: 'Stop now!' }
}
Expand Down
2 changes: 1 addition & 1 deletion examples/thunk-thenjs.js
@@ -1,6 +1,6 @@
'use strict'

var thunk = require('../thunks.js')()
var thunk = require('..').thunk
var Then = require('thenjs')

Then(thunk).then(function (cont, value) {
Expand Down
2 changes: 1 addition & 1 deletion examples/thunkify.js
@@ -1,6 +1,6 @@
'use strict'

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

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -115,6 +115,7 @@ interface Thunk {
race(...args: Array<thunkable>): ThunkFunction;
race(array: Array<thunkable>): ThunkFunction;
persist(thunkable: thunkable): ThunkFunction;
promise(thunkable: thunkable): PromiseLike;
thunkify(FnWithCb: FunctionWithCallback): (...args: Array<primitives>) => ThunkFunction;
lift(fn: (...args: Array<primitives>) => primitives): (...args: Array<thunkable>) => ThunkFunction;
delay(Time?: number): ThunkFunction;
Expand Down

0 comments on commit 47e5833

Please sign in to comment.