From a75a15b61c6e4d675d84729523fdb8ac8e452f0c Mon Sep 17 00:00:00 2001 From: yisraelx Date: Wed, 13 Sep 2017 22:21:57 +0300 Subject: [PATCH] feat: add method sleep This method is same as the previous "delay" method(same api, just the name changed). --- modules/-all/index.ts | 1 + modules/-all/package.json | 1 + modules/-prototype/index.ts | 1 + modules/-prototype/package.json | 1 + modules/sleep/index.ts | 44 +++++++++++++++++++++++++++++++++ modules/sleep/package.json | 38 ++++++++++++++++++++++++++++ test/sleep.spec.ts | 28 +++++++++++++++++++++ 7 files changed, 114 insertions(+) create mode 100644 modules/sleep/index.ts create mode 100644 modules/sleep/package.json create mode 100644 test/sleep.spec.ts diff --git a/modules/-all/index.ts b/modules/-all/index.ts index a4b1769..5efb5e3 100644 --- a/modules/-all/index.ts +++ b/modules/-all/index.ts @@ -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'; diff --git a/modules/-all/package.json b/modules/-all/package.json index bba20df..f7e12d9 100644 --- a/modules/-all/package.json +++ b/modules/-all/package.json @@ -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", diff --git a/modules/-prototype/index.ts b/modules/-prototype/index.ts index f56cc74..07faa46 100644 --- a/modules/-prototype/index.ts +++ b/modules/-prototype/index.ts @@ -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'; diff --git a/modules/-prototype/package.json b/modules/-prototype/package.json index e93d021..cf188b4 100644 --- a/modules/-prototype/package.json +++ b/modules/-prototype/package.json @@ -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", diff --git a/modules/sleep/index.ts b/modules/sleep/index.ts new file mode 100644 index 0000000..ed4344e --- /dev/null +++ b/modules/sleep/index.ts @@ -0,0 +1,44 @@ +/** + * @module @promises/sleep + * @copyright © 2017 Yisrael Eliav (https://github.com/yisraelx) + * @license MIT + */ + +import Promises from '@promises/core'; +import { OptionalPromise } from '@promises/interfaces'; + +/** + * @example + * + * ```typescript + * let promises: Promises = Promises.resolve('foo'); + * sleep(promises, 3000).then((result: string) => { + * console.log(result); // result => 'foo' + * }); + * ``` + */ +function sleep(value?: OptionalPromise, ms?: number): Promises { + return Promises.resolve(value).then(() => new Promises((resolve) => { + setTimeout(() => resolve(value), ms); + })) as Promises; +} + +export default sleep; + +Promises._setOnPrototype('sleep', sleep); + +declare module '@promises/core' { + interface Promises { + /** + * @example + * + * ```typescript + * let promises: Promises = Promises.resolve('foo'); + * promises.sleep(3000).then((result: string) => { + * console.log(result); // result => 'foo' + * }); + * ``` + */ + sleep(ms?: number): Promises; + } +} \ No newline at end of file diff --git a/modules/sleep/package.json b/modules/sleep/package.json new file mode 100644 index 0000000..a3ed36a --- /dev/null +++ b/modules/sleep/package.json @@ -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" + } +} diff --git a/test/sleep.spec.ts b/test/sleep.spec.ts new file mode 100644 index 0000000..d9baf60 --- /dev/null +++ b/test/sleep.spec.ts @@ -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 = 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(); + }); + }); +}); \ No newline at end of file