Skip to content

Commit

Permalink
timeout: doc
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Jun 1, 2016
1 parent 7f174c2 commit 1063c32
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.0.3 2016-06-01
- add `onCancel` to support clean up
- use esformatter, remove jsbeautify

## v0.0.2 2016-05-15
- make `ptimeout.TimeoutError` inherits from `Error`

Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ $ npm i promise.timeout --save

## API

`ptimeout(fn, timeout)`
`ptimeout(fn, timeout, cancel)`
- `fn` the async function
- `timeout` in ms
- `cancel` Boolean, whether support onCancel

```js
var ptimeout = require('promise.timeout');
Expand Down Expand Up @@ -52,6 +53,37 @@ _50.should.be.ok();
_50.should.equal(20);
```

### onCancel

1. pass `cancel = true` to `ptimeout(fn, ms, cancel)`
2. use `onCancel` para to register clean callback

```js
var ptimeout = require('promise.timeout');

// a function will cost 20ms
function test(onCancel) {
return new Promise(function(resolve, reject) {
const timer = setTimeout(function() {
resolve(20);
}, 20);

// custom clean
onCancel && onCancel(() => {
clearTimeout(timer);
});
});
}

const test10 = ptimeout(test, 10, true); // enable cancel
try {
yield test10();
} catch (e) {
e.should.ok();
}
```


## Changelog

[CHANGELOG.md](CHANGELOG.md)
Expand Down
4 changes: 2 additions & 2 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ describe('simple use', function() {
resolve(20);
}, 20);

// clean
onCancel && onCancel(function() {
// custom clean
onCancel && onCancel(() => {
clearTimeout(timer);
});
});
Expand Down

0 comments on commit 1063c32

Please sign in to comment.