UNPKG

1.34 kBPlain TextView Raw
1import { EventEmitter } from 'events'
2import { setTimeout, clearTimeout } from 'timers';
3import { polyfill } from 'es6-promise'
4
5polyfill();
6
7
8
9export function Promisify<T>(o: T | PromiseLike<T>): PromiseLike<T>
10{
11 return Promise.resolve(o);
12}
13
14export type ResolveHandler<T, TResult> = (value: T) => TResult | PromiseLike<TResult>
15export type RejectHandler<TResult> = (reason: any) => void | TResult | PromiseLike<TResult>;
16
17export function isPromiseLike<T>(o: T | PromiseLike<T>): o is PromiseLike<T>
18{
19 return o && o['then'] && typeof (o['then']) == 'function';
20}
21
22export function when<T>(promises: PromiseLike<T>[]): PromiseLike<T[]>
23{
24 return Promise.all(promises);
25}
26
27export 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
49export enum PromiseStatus
50{
51 Pending = 0,
52 Resolved = 1,
53 Rejected = 2
54}
\No newline at end of file