1 | import { EventEmitter } from 'events'
|
2 | import { setTimeout, clearTimeout } from 'timers';
|
3 | import { polyfill } from 'es6-promise'
|
4 |
|
5 | polyfill();
|
6 |
|
7 |
|
8 |
|
9 | export function Promisify<T>(o: T | PromiseLike<T>): PromiseLike<T>
|
10 | {
|
11 | return Promise.resolve(o);
|
12 | }
|
13 |
|
14 | export type ResolveHandler<T, TResult> = (value: T) => TResult | PromiseLike<TResult>
|
15 | export type RejectHandler<TResult> = (reason: any) => void | TResult | PromiseLike<TResult>;
|
16 |
|
17 | export function isPromiseLike<T>(o: T | PromiseLike<T>): o is PromiseLike<T>
|
18 | {
|
19 | return o && o['then'] && typeof (o['then']) == 'function';
|
20 | }
|
21 |
|
22 | export function when<T>(promises: PromiseLike<T>[]): PromiseLike<T[]>
|
23 | {
|
24 | return Promise.all(promises);
|
25 | }
|
26 |
|
27 | export function whenOrTimeout<T>(promise: PromiseLike<T>, timeoutInMs: number): PromiseLike<T>
|
28 | {
|
29 | return new Promise<T>((resolve, reject) =>
|
30 | {
|
31 | var timedOut = false;
|
32 | var timeOut = setTimeout(function ()
|
33 | {
|
34 | timedOut = true;
|
35 | reject('timeout');
|
36 | }, timeoutInMs);
|
37 | promise.then(function (data)
|
38 | {
|
39 | clearTimeout(timeOut);
|
40 | resolve(data);
|
41 | }, function (rejection)
|
42 | {
|
43 | clearTimeout(timeOut);
|
44 | reject(rejection);
|
45 | });
|
46 | })
|
47 | }
|
48 |
|
49 | export enum PromiseStatus
|
50 | {
|
51 | Pending = 0,
|
52 | Resolved = 1,
|
53 | Rejected = 2
|
54 | } |
\ | No newline at end of file |