smartpromise/ts/index.ts

95 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-01-17 14:28:28 +00:00
export interface IResolve<T> {
2019-09-11 13:38:37 +00:00
(value?: T | PromiseLike<T>): void;
2017-01-17 14:28:28 +00:00
}
export interface IReject {
2018-03-16 09:24:12 +00:00
(reason?: any): void;
2017-01-17 14:28:28 +00:00
}
2018-03-16 09:24:12 +00:00
export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected';
2017-01-17 14:28:28 +00:00
export class Deferred<T> {
2019-09-11 13:37:17 +00:00
public promise: Promise<T>;
public resolve: IResolve<T>;
public reject: IReject;
public status: TDeferredStatus;
public startedAt: number;
public stoppedAt: number;
public get duration(): number {
if (this.stoppedAt) {
return this.stoppedAt - this.startedAt;
} else {
return Date.now() - this.startedAt;
}
}
2018-03-16 09:24:12 +00:00
constructor() {
2017-07-06 12:10:18 +00:00
this.promise = new Promise<T>((resolve, reject) => {
2019-09-11 13:37:17 +00:00
this.resolve = (valueArg: T | PromiseLike<T>) => {
this.status = 'fulfilled';
this.stoppedAt = Date.now();
resolve(valueArg);
};
this.reject = (reason: any) => {
this.status = 'rejected';
this.stoppedAt = Date.now();
reject(reason);
};
this.startedAt = Date.now();
2018-03-16 09:24:12 +00:00
this.status = 'pending';
});
2017-07-06 12:10:18 +00:00
}
2017-01-17 14:28:28 +00:00
}
2020-10-15 18:02:14 +00:00
export const defer = <T>() => {
2018-03-16 09:24:12 +00:00
return new Deferred<T>();
};
2017-01-17 14:28:28 +00:00
/**
* Creates a new resolved promise for the provided value.
*/
2020-10-15 18:02:14 +00:00
export const resolvedPromise = <T>(value?: T): Promise<T> => {
2018-03-16 09:24:12 +00:00
return Promise.resolve(value);
};
/**
* Creates a new rejected promise for the provided reason.
*/
2020-10-15 18:02:14 +00:00
export const rejectedPromise = (err) => {
2018-03-16 09:24:12 +00:00
return Promise.reject(err);
};
2017-01-28 15:37:54 +00:00
interface IAsyncFunction<T> {
2019-03-26 11:07:12 +00:00
(someArg: T): Promise<T>;
}
2017-07-06 12:55:42 +00:00
2020-10-15 18:02:14 +00:00
/**
* accepts an array of inputs and a function that accepts the input.
* runs all items with the function and returns the result array when all items have run
* @param inputArg
* @param functionArg
*/
export const map = async <T>(inputArg: T[], functionArg: IAsyncFunction<T>) => {
const promiseArray: Promise<any>[] = [];
const resultArray = [];
for (const item of inputArg) {
const promise: Promise<any> = functionArg(item);
2018-03-16 09:24:12 +00:00
promiseArray.push(promise);
2020-10-15 18:02:14 +00:00
promise.then((x) => {
2018-03-16 09:24:12 +00:00
resultArray.push(x);
});
2017-07-06 12:10:18 +00:00
}
2018-03-16 09:24:12 +00:00
await Promise.all(promiseArray);
return resultArray;
};
2020-10-15 18:02:14 +00:00
2020-10-16 01:39:19 +00:00
export const timeoutWrap = <T = any>(promiseArg: Promise<T>, timeoutInMs: number) => {
2020-10-16 02:06:50 +00:00
return new Promise<T>((resolve, reject) => {
2020-10-15 18:02:14 +00:00
setTimeout(() => {
reject(new Error('timeout'));
2020-10-16 01:39:19 +00:00
}, timeoutInMs);
promiseArg.then(resolve, reject);
2020-10-15 18:02:14 +00:00
});
};