Skip to content

Commit

Permalink
added timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal Golani committed Dec 21, 2015
1 parent 9dbe31a commit 9652eac
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,37 @@ Can be used as a yieldable with [co](https://github.com/tj/co) or in [koa](koajs
Basic example:

```javascript
var wait = require('wait-then');
var wait = require('wait-then'),
timeout = wait.timeout;

wait(2000).then(function() {
console.log('This is logged after 2 seconds');
});

timeout(2000).catch(e) {
console.log('This is logged after 2 seconds');
}
```

Example with `co`:

```javascript
var wait = require('wait-then'),
timeout = wait.timeout,
co = require('co');

co(function *() {
yield wait(1000);
console.log('This is logged after 1 second');
});

co(function *() {
try {
yield timeout(1000);
} catch (e) {
console.log('This is logged after 1 second');
}
});
```

The generator function passed into `co` can be yielded in a middleware in `koa`.
Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ module.exports = function(ms) {
resolve();
}, ms);
});
};
};

module.exports.timeout = function(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('TIMED OUT'));
}, ms);
});
}
46 changes: 44 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";

var co = require('co'),
wait = require('../lib');
wait = require('../lib'),
timeout = wait.timeout;

require('chai').should();

describe('wait-then', function() {
describe('wait', function() {

it('should return an instance of Promise', function() {
(wait(1) instanceof Promise).should.be.true;
Expand Down Expand Up @@ -41,4 +42,45 @@ describe('wait-then', function() {
});
});

});


describe('timeout', function() {

it('should return an instance of Promise', function() {
(timeout(1) instanceof Promise).should.be.true;
(timeout(1).then instanceof Function).should.be.true;
});

it('should reject after the given time', function(done) {
var t = 100,
now = Date.now();

timeout(t)
.then(function() {
done(new Error('resolved instead of rejecting'));
})
.catch(function(e) {
(Date.now() - now).should.be.at.least(t).and.at.most(t * 1.1); // 10% margin
e.message.should.equal('TIMED OUT');
done();
});
});

it('should work with co', function(done) {
co(function *() {
var t = 200,
now = Date.now();

try {
yield timeout(t);
done(new Error('resolved instead of rejecting'));
} catch (e) { // because the error is thrown in the context of setTimeout and cannot be caught by 'it' otherwise
(Date.now() - now).should.be.at.least(t).and.at.most(t * 1.1); // 10% margin
e.message.should.equal('TIMED OUT');
done();
}
});
});

});

0 comments on commit 9652eac

Please sign in to comment.