diff --git a/CHANGELOG.md b/CHANGELOG.md index 47742e6..6e784b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/README.md b/README.md index 1f30469..e8bb7ee 100644 --- a/README.md +++ b/README.md @@ -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'); @@ -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) diff --git a/test/simple.js b/test/simple.js index d3d2fc6..e96bb3d 100644 --- a/test/simple.js +++ b/test/simple.js @@ -49,8 +49,8 @@ describe('simple use', function() { resolve(20); }, 20); - // clean - onCancel && onCancel(function() { + // custom clean + onCancel && onCancel(() => { clearTimeout(timer); }); });