fix(typescript): tighten TypeScript typings and modernize project tooling configuration
This commit is contained in:
+18
-24
@@ -3,23 +3,24 @@ import { defer } from './smartpromise.classes.deferred.js';
|
||||
export * from './smartpromise.classes.cumulativedeferred.js';
|
||||
export * from './smartpromise.classes.deferred.js';
|
||||
|
||||
|
||||
export function resolvedPromise(): Promise<void>;
|
||||
export function resolvedPromise<T>(value: T): Promise<T>;
|
||||
/**
|
||||
* Creates a new resolved promise for the provided value.
|
||||
*/
|
||||
export const resolvedPromise = <T>(value?: T): Promise<T> => {
|
||||
export function resolvedPromise<T>(value?: T): Promise<T | void> {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new rejected promise for the provided reason.
|
||||
*/
|
||||
export const rejectedPromise = (err) => {
|
||||
export const rejectedPromise = (err: unknown): Promise<never> => {
|
||||
return Promise.reject(err);
|
||||
};
|
||||
|
||||
interface IAsyncFunction<T> {
|
||||
(someArg: T): Promise<T>;
|
||||
interface IAsyncFunction<TInput, TOutput> {
|
||||
(someArg: TInput): Promise<TOutput>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,26 +29,19 @@ interface IAsyncFunction<T> {
|
||||
* @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);
|
||||
promiseArray.push(promise);
|
||||
promise.then((x) => {
|
||||
resultArray.push(x);
|
||||
});
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
return resultArray;
|
||||
export const map = async <TInput, TOutput = TInput>(
|
||||
inputArg: TInput[],
|
||||
functionArg: IAsyncFunction<TInput, TOutput>
|
||||
): Promise<TOutput[]> => {
|
||||
return Promise.all(inputArg.map(functionArg));
|
||||
};
|
||||
|
||||
export const timeoutWrap = async <T = any>(
|
||||
export const timeoutWrap = async <T = unknown>(
|
||||
promiseArg: Promise<T>,
|
||||
timeoutInMsArg: number,
|
||||
rejectArg = true
|
||||
) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
): Promise<T | null> => {
|
||||
return new Promise<T | null>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (rejectArg) {
|
||||
reject(new Error('timeout'));
|
||||
@@ -59,14 +53,14 @@ export const timeoutWrap = async <T = any>(
|
||||
});
|
||||
};
|
||||
|
||||
export const timeoutAndContinue = async <T = any>(
|
||||
export const timeoutAndContinue = async <T = unknown>(
|
||||
promiseArg: Promise<T>,
|
||||
timeoutInMsArg = 60000
|
||||
) => {
|
||||
): Promise<T | null> => {
|
||||
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
||||
};
|
||||
|
||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
||||
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]): Promise<boolean> => {
|
||||
const done = defer<boolean>();
|
||||
for (const promiseArg of promisesArg) {
|
||||
promiseArg.then((resultArg) => {
|
||||
|
||||
Reference in New Issue
Block a user