Skip to content

Commit

Permalink
feat: add method wait
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelx committed Mar 4, 2018
1 parent 4fcf07f commit 7b68d41
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/-all/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export { default as timer } from '@promises/timer/add';
export { default as timesParallel } from '@promises/times-parallel/add';
export { default as timesSeries } from '@promises/times-series/add';
export { default as toCallback } from '@promises/to-callback/add';
export { default as wait } from '@promises/wait/add';
export { default as whileParallel } from '@promises/while-parallel/add';
export { default as whileSeries } from '@promises/while-series/add';
export { default as wrap } from '@promises/wrap/add';
1 change: 1 addition & 0 deletions modules/-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@promises/times-parallel": "^0.2.0",
"@promises/times-series": "^0.2.0",
"@promises/to-callback": "^0.2.0",
"@promises/wait": "NEXT-PLACEHOLDER",
"@promises/while-parallel": "NEXT-PLACEHOLDER",
"@promises/while-series": "NEXT-PLACEHOLDER",
"@promises/wrap": "^0.2.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 @@ -30,3 +30,4 @@ export { default as someSeries } from '@promises/some-series/add';
export { default as spread } from '@promises/spread/add';
export { default as timer } from '@promises/timer/add';
export { default as toCallback } from '@promises/to-callback/add';
export { default as wait } from '@promises/wait/add';
3 changes: 2 additions & 1 deletion modules/-prototype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@promises/some-series": "^0.2.0",
"@promises/spread": "^0.2.0",
"@promises/timer": "^0.2.0",
"@promises/to-callback": "^0.2.0"
"@promises/to-callback": "^0.2.0",
"@promises/wait": "NEXT-PLACEHOLDER"
}
}
24 changes: 24 additions & 0 deletions modules/wait/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Promises from '@promises/core';
import { IOptionalPromise } from '@promises/interfaces';
import wait from './';

Promises._setOnPrototype('wait', wait);

export default wait;

declare module '@promises/core' {
interface Promises<T> {
/**
* @example
*
* ```typescript
* let promises: Promises<string> = Promises.resolve<string>('foo');
* let count: number = 0;
* promises.wait(() => count++ === 3, 1000).then((result: string) => {
* console.log(result); // result => 'foo'
* });
* ```
*/
wait(this: Promises<T>, test?: (value: T) => IOptionalPromise<boolean>, ms?: number): Promises<T>;
}
}
35 changes: 35 additions & 0 deletions modules/wait/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @module @promises/wait
* @copyright © 2017 Yisrael Eliav <yisraelx@gmail.com> (https://github.com/yisraelx)
* @license MIT
*/

import { IOptionalPromise } from '@promises/interfaces';

/**
* @example
*
* ```typescript
* let promise: Promise<string> = Promise.resolve<string>('foo');
* let count: number = 0;
* wait(promise, () => count++ === 3, 1000).then((result: string) => {
* console.log(result); // result => 'foo'
* });
* ```
*/
function wait<T>(value: IOptionalPromise<T>, test?: (value: T) => IOptionalPromise<boolean>, ms?: number): Promise<T> {
return Promise.resolve(value).then((value: T) => {
return new Promise<T>((resolve, reject) => {

let next = () => {
Promise.resolve(value).then(test).then((result: boolean) => {
result ? resolve(value) : setTimeout(next, ms);
}).catch(reject);
};

next();
});
});
}

export default wait;
40 changes: 40 additions & 0 deletions modules/wait/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@promises/wait",
"version": "NEXT-PLACEHOLDER",
"description": "Wait is package from Promises library",
"main": "es5.js",
"browser": "umd.min.js",
"module": "index.js",
"es2015": "index.js",
"typings": "index.d.ts",
"bundle": "bundle.min.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"
},
"optionalDependencies": {
"@promises/core": "^0.2.0"
},
"dependencies": {
"@promises/interfaces": "^0.2.0"
}
}
26 changes: 26 additions & 0 deletions test/wait.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import wait from '@promises/wait';

describe('wait', () => {
it('should be resolve after 5 times', () => {
let promise = Promise.resolve('foo');
let count = 0;
return wait(promise, (value) => {
expect(value).toBe('foo');
return count++ === 5;
}).then((value) => {
expect(value).toBe('foo');
expect(count).toBe(6);
});
});

it('should be reject if there test error', () => {
let promise = Promise.resolve('foo');
return wait(promise, () => {
throw 'reject';
}).then(() => {
throw 'resolve';
}).catch((error) => {
expect(error).toBe('reject');
});
});
});

0 comments on commit 7b68d41

Please sign in to comment.