Skip to content

Commit

Permalink
feat: add method sleep
Browse files Browse the repository at this point in the history
This method is same as the previous "delay" method(same api, just the name changed).
  • Loading branch information
yisraelx committed Sep 13, 2017
1 parent d10faf3 commit a75a15b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/-all/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { default as rejectSeries } from '@promises/reject-series';
export { default as reset } from '@promises/reset';
export { default as retry, RetryOptions, RetryFilterInfo, RetryTimeInfo } from '@promises/retry';
export { default as series } from '@promises/series';
export { default as sleep } from '@promises/sleep';
export { default as someParallel } from '@promises/some-parallel';
export { default as someSeries } from '@promises/some-series';
export { default as spread } from '@promises/spread';
Expand Down
1 change: 1 addition & 0 deletions modules/-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@promises/reset": "^0.1.0",
"@promises/retry": "^0.1.0",
"@promises/series": "^0.1.0",
"@promises/sleep": "NEXT-PLACEHOLDER",
"@promises/some-parallel": "^0.1.0",
"@promises/some-series": "^0.1.0",
"@promises/spread": "^0.1.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-prototype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { default as rejectParallel } from '@promises/reject-parallel';
export { default as rejectSeries } from '@promises/reject-series';
export { default as reset } from '@promises/reset';
export { default as retry, RetryOptions, RetryFilterInfo, RetryTimeInfo } from '@promises/retry';
export { default as sleep } from '@promises/sleep';
export { default as someParallel } from '@promises/some-parallel';
export { default as someSeries } from '@promises/some-series';
export { default as spread } from '@promises/spread';
Expand Down
1 change: 1 addition & 0 deletions modules/-prototype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@promises/reject-series": "^0.1.0",
"@promises/reset": "^0.1.0",
"@promises/retry": "^0.1.0",
"@promises/sleep": "NEXT-PLACEHOLDER",
"@promises/some-parallel": "^0.1.0",
"@promises/some-series": "^0.1.0",
"@promises/spread": "^0.1.0",
Expand Down
44 changes: 44 additions & 0 deletions modules/sleep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @module @promises/sleep
* @copyright © 2017 Yisrael Eliav <yisraelx@gmail.com> (https://github.com/yisraelx)
* @license MIT
*/

import Promises from '@promises/core';
import { OptionalPromise } from '@promises/interfaces';

/**
* @example
*
* ```typescript
* let promises: Promises<string> = Promises.resolve<string>('foo');
* sleep(promises, 3000).then((result: string) => {
* console.log(result); // result => 'foo'
* });
* ```
*/
function sleep<T>(value?: OptionalPromise<T>, ms?: number): Promises<T> {
return Promises.resolve(value).then(() => new Promises<T>((resolve) => {
setTimeout(() => resolve(value), ms);
})) as Promises<T>;
}

export default sleep;

Promises._setOnPrototype('sleep', sleep);

declare module '@promises/core' {
interface Promises<T> {
/**
* @example
*
* ```typescript
* let promises: Promises<string> = Promises.resolve<string>('foo');
* promises.sleep(3000).then((result: string) => {
* console.log(result); // result => 'foo'
* });
* ```
*/
sleep(ms?: number): Promises<T>;
}
}
38 changes: 38 additions & 0 deletions modules/sleep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@promises/sleep",
"version": "NEXT-PLACEHOLDER",
"description": "Sleep is package from Promises library",
"main": "umd.js",
"browser": "umd.js",
"module": "es5.js",
"es2015": "index.js",
"typings": "index.d.ts",
"bundle": "bundle.js",
"author": {
"name": "Yisrael Eliev",
"url": "https://github.com/yisraelx",
"email": "yisraelx@gmail.com"
},
"license": "MIT",
"keywords": [
"promise",
"promises",
"utility",
"modules",
"async",
"await",
"deferred"
],
"homepage": "https://github.com/yisraelx/promises#readme",
"repository": {
"type": "git",
"url": "https://github.com/yisraelx/promises.git"
},
"bugs": {
"url": "https://github.com/yisraelx/promises/issues"
},
"dependencies": {
"@promises/core": "^0.1.0",
"@promises/interfaces": "^0.1.0"
}
}
28 changes: 28 additions & 0 deletions test/sleep.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Promises from '@promises/core';
import sleep from '@promises/sleep';
import delay from '@promises/delay';

describe('sleep', () => {

it('should be pass the value after sleep', () => {
let promise: Promises<string> = Promises.resolve('foo');
let sleepPromises = sleep(promise, 1);

return sleepPromises.then((result: string) => {
expect(result).toBe('foo');
});
});

it('should be promise reject not sleep', () => {
let pass = false;
let promise = Promises.reject('error');

sleep(promise, 5).catch(() => {
pass = true;
});

return delay(1).then(() => {
expect(pass).toBeTruthy();
});
});
});

0 comments on commit a75a15b

Please sign in to comment.