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

Commit 23eefcf

Browse files
committed
add test, documents
1 parent 5af390c commit 23eefcf

File tree

11 files changed

+296
-18
lines changed

11 files changed

+296
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ build/Release
2525
# Dependency directory
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
28+
29+
debug

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: node_js
22
node_js:
3-
- "0.10"
4-
- "0.12"
53
- "4"
64
- "5"
75
sudo: false

README.md

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
thunk-loop
22
====
3-
Asynchronous tasks loop.
3+
Asynchronous tasks loop (while (true) { ... }).
44

55
[![NPM version][npm-image]][npm-url]
66
[![Build Status][travis-image]][travis-url]
77

88
## [thunks](https://github.com/thunks/thunks)
99

1010
## Demo
11-
11+
**Infinite Clock:**
1212
```js
1313
const thunk = require('thunks')()
1414
const thunkLoop = require('..')
1515

16-
var count = 10
16+
var i = 0
1717
thunkLoop(function *() {
1818
yield thunk.delay(1000)
19-
if (!--count) throw new Error('End!')
20-
console.log(': ' + count)
21-
19+
i = ++i % 60
20+
console.log(i)
2221
return true
23-
})(function (err, res) {
24-
console.log(err, res)
22+
})(function (_) {
23+
// will never reach here.
2524
})
2625
```
2726

@@ -33,6 +32,82 @@ const thunkLoop = require('thunk-loop')
3332

3433
### thunkLoop(iter[, errorHandle])
3534

35+
return thunk function. You should run the thunk function because of thunk's lazy evaluation. If iter is infinite, you will not get the last result except error occured.
36+
37+
- `iter`: {Function}, it is your task loop, can be sync or async task. If `iter`'s result is `true`, the loop will continue, otherwise the loop will terminate.
38+
39+
**Sync function:**
40+
```js
41+
var i = 1000
42+
thunkLoop(function () {
43+
if (--i) return true
44+
return 'OK'
45+
})(function (err, res) {
46+
console.log(err, res, i) // null, 'OK', 0
47+
})
48+
```
49+
50+
**Promise:**
51+
```js
52+
var i = 1000
53+
thunkLoop(function () {
54+
if (--i) return Promise.resolve(true)
55+
return Promise.resolve('OK')
56+
})(function (err, res) {
57+
console.log(err, res, i) // null, 'OK', 0
58+
})
59+
```
60+
61+
**Generator function:**
62+
```js
63+
var i = 1000
64+
thunkLoop(function *() {
65+
// yield thunk or promise
66+
if (--i) return yield thunk(true)
67+
return yield Promise.resolve('OK')
68+
})(function (err, res) {
69+
console.log(err, res, i) // null, 'OK', 0
70+
})
71+
```
72+
73+
- `errorHandle`: {Function}, it is optional, can be sync or async function. It is used to catch the exception from `iter`.
74+
75+
If `errorHandle` omit. the exception from `iter` will terminate loop:
76+
```js
77+
thunkLoop(function *() {
78+
throw new Error('error!')
79+
})(function (err, res) {
80+
console.log(err.message, ) 'error!'
81+
})
82+
```
83+
84+
If `errorHandle` return `true`. the exception from `iter` will ignore and loop will continue:
85+
```js
86+
var i = 1000
87+
thunkLoop(function () {
88+
if (--i) throw new Error('test')
89+
return 'OK'
90+
}, function (err) {
91+
console.log(err.message) // 'test'
92+
return true
93+
})(function (err, res) {
94+
console.log(err, res, i) // null, 'OK', 0
95+
})
96+
```
97+
98+
If `errorHandle` throw error:
99+
```js
100+
var i = 1000
101+
thunkLoop(function () {
102+
if (--i) throw new Error('test')
103+
return 'OK'
104+
}, function (err) {
105+
console.log(err.message) // 'test'
106+
throw new Error('errorHandle error')
107+
})(function (err, res) {
108+
console.log(err.message, i) // 'errorHandle error', 999
109+
})
110+
```
36111

37112
[npm-url]: https://npmjs.org/package/thunk-loop
38113
[npm-image]: http://img.shields.io/npm/v/thunk-loop.svg

examples/clock.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
const thunk = require('thunks')()
3+
const thunkLoop = require('..')
4+
5+
var i = 0
6+
thunkLoop(function *() {
7+
yield thunk.delay(1000)
8+
i = ++i % 60
9+
console.log(i)
10+
return true
11+
})(function (_) {
12+
// will never reach here.
13+
})

examples/errorhandle-loop.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const thunkLoop = require('..')
2+
3+
var i = 1000
4+
thunkLoop(function () {
5+
if (--i) throw new Error('test')
6+
return 'OK'
7+
}, function (err) {
8+
console.log(err.message)
9+
return true
10+
})(function (err, res) {
11+
console.log(err, res, i) // null, 'OK', 0
12+
})
13+
14+
// thunkLoop(function () {
15+
// if (--i) throw new Error('test')
16+
// return 'OK'
17+
// }, function (err) {
18+
// console.log(err.message) // 'test'
19+
// throw new Error('errorHandle error')
20+
// })(function (err, res) {
21+
// console.log(err, res, i) // null, 'OK', 0
22+
// })

examples/generator-loop.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const thunkLoop = require('..')
2+
const thunk = require('thunks')()
3+
4+
var i = 1000
5+
thunkLoop(function *() {
6+
// yield thunk or promise
7+
if (--i) return yield thunk(true)
8+
return yield Promise.resolve('OK')
9+
})(function (err, res) {
10+
console.log(err, res, i) // null, 'OK', 0
11+
})

examples/promise-loop.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const thunkLoop = require('..')
2+
3+
var i = 1000
4+
thunkLoop(function () {
5+
if (--i) return Promise.resolve(true)
6+
return Promise.resolve('OK')
7+
})(function (err, res) {
8+
console.log(err, res, i) // null, 'OK', 0
9+
})

examples/sync-loop.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const thunkLoop = require('..')
2+
3+
var i = 1000
4+
thunkLoop(function () {
5+
if (--i) return true
6+
return 'OK'
7+
})(function (err, res) {
8+
console.log(err, res, i) // null, 'OK', 0
9+
})

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ module.exports = thunkLoop.loop = thunkLoop
1111

1212
function thunkLoop (iter, errorHandle) {
1313
errorHandle = errorHandle || nOop
14-
let handle = (err, res) => err == null ? res : thunk(errorHandle(err))
14+
1515
return thunk(function *() {
1616
while (true) {
17-
let res = yield thunk(iter())(handle)
17+
let res = null
18+
try {
19+
res = yield iter()
20+
} catch (e) {
21+
res = yield errorHandle(e)
22+
}
1823
if (res !== true) return res
1924
}
2025
})

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "thunk-loop",
3-
"version": "0.1.0",
4-
"description": "Asynchronous tasks loop.",
3+
"version": "1.0.0",
4+
"description": "Asynchronous tasks loop (while (true) { ... }).",
55
"authors": [
66
"Yan Qing <admin@zensh.com>"
77
],
@@ -19,6 +19,9 @@
1919
"thunk",
2020
"task"
2121
],
22+
"engines": {
23+
"node": ">= 4.0"
24+
},
2225
"license": "MIT",
2326
"bugs": {
2427
"url": "https://github.com/thunks/thunk-loop/issues"

0 commit comments

Comments
 (0)