@tib/defer
v0.1.2
Published
Create a deferred promise
Downloads
10
Readme
@tib/defer
Create a deferred promise
Usage
Promised delay
import {Defer} from '@tib/defer';
function delay(ms) {
const deferred = new Defer();
setTimeout(deferred.resolve, ms, '🦄');
return deferred.promise;
}
(async () => {
console.log(await delay(100));
//=> '🦄'
})();
The above is just an example. Use delay if you need to delay a promise.
Testing for callback/event
You can write test cases more elegant with @tib/defer
for callback/event
testing
class FakeStream extends EventEmitter {
write(data: any, delays = 10) {
setTimeout(() => this.emit('data', data), delays);
}
}
describe('something', () => {
it('should work with callbacks', async () => {
const deferred = new Defer();
const stream = new FakeStream();
stream.once('data', deferred.resolve);
stream.write('🦄');
const data = await deferred;
expect(data).equal('🦄');
});
});
API
new Defer()
Returns a promise
instance with resolve()
and reject()
functions.
Related
- Deferred that extends Promise
- p-defer - Create a deferred promise