-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |