Skip to content

Commit

Permalink
Add delay.reject
Browse files Browse the repository at this point in the history
  • Loading branch information
ariporad committed Dec 24, 2015
1 parent 7e72c80 commit 07f43e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ module.exports = function (ms) {

return thunk;
};

module.exports.reject = function (ms, value) {
if (arguments.length === 1) {
return new Promise(function (resolve, reject) {
setTimeout(reject.bind(null, value), ms);
});
}
return module.exports.reject.bind(null, ms);
};
33 changes: 30 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,41 @@ import inRange from 'in-range';
import fn from './';

test('promise', async t => {
t.plan(1);
const end = timeSpan();
await fn(50);
t.true(inRange(end(), 30, 70));
t.true(inRange(end(), 30, 70), 'should be delayed');
});

test('thunk', async t => {
t.plan(2);
const end = timeSpan();
const result = await Promise.resolve('foo').then(fn(50));
t.true(inRange(end(), 30, 70));
t.is(result, 'foo');
t.true(inRange(end(), 30, 70), 'should be delayed');
t.is(result, 'foo', 'should pass through the value');
});

test.cb('.reject with two arguments', t => {
t.plan(2);
const error = new Error('foo');
const end = timeSpan();
fn.reject(50, error)
.catch(err => {
t.true(inRange(end(), 30, 70), 'should be delayed');
t.is(err, error, 'promise should be rejected with the second argument');
t.end();
});
});

test.cb('.reject with one argument (thunk)', t => {
t.plan(2);
const val = 'foo';
const end = timeSpan();
Promise.resolve(val)
.then(fn.reject(50))
.catch(err => {
t.true(inRange(end(), 30, 70), 'should be delayed');
t.is(err, val, 'promise should be rejected with the resolved value');
t.end();
});
});

0 comments on commit 07f43e2

Please sign in to comment.