2023-04-04 20:22:57 +02:00
|
|
|
import { defer } from './smartpromise.classes.deferred.js';
|
2017-01-17 15:28:28 +01:00
|
|
|
|
2023-04-04 20:35:54 +02:00
|
|
|
export * from './smartpromise.classes.cumulativedeferred.js';
|
2023-04-04 20:22:57 +02:00
|
|
|
export * from './smartpromise.classes.deferred.js';
|
2017-07-27 18:17:37 +02:00
|
|
|
|
2017-01-17 15:28:28 +01:00
|
|
|
|
2017-01-20 21:58:53 +01: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 10:24:12 +01:00
|
|
|
return Promise.resolve(value);
|
|
|
|
};
|
2017-01-20 21:58:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new rejected promise for the provided reason.
|
|
|
|
*/
|
2020-10-15 18:02:14 +00:00
|
|
|
export const rejectedPromise = (err) => {
|
2018-03-16 10:24:12 +01:00
|
|
|
return Promise.reject(err);
|
|
|
|
};
|
2017-01-28 16:37:54 +01:00
|
|
|
|
2019-03-26 12:02:01 +01:00
|
|
|
interface IAsyncFunction<T> {
|
2019-03-26 12:07:12 +01:00
|
|
|
(someArg: T): Promise<T>;
|
2019-03-26 12:02:01 +01:00
|
|
|
}
|
2017-07-06 14:55:42 +02: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 10:24:12 +01:00
|
|
|
promiseArray.push(promise);
|
2020-10-15 18:02:14 +00:00
|
|
|
promise.then((x) => {
|
2018-03-16 10:24:12 +01:00
|
|
|
resultArray.push(x);
|
|
|
|
});
|
2017-07-06 14:10:18 +02:00
|
|
|
}
|
2018-03-16 10:24:12 +01:00
|
|
|
await Promise.all(promiseArray);
|
|
|
|
return resultArray;
|
|
|
|
};
|
2020-10-15 18:02:14 +00:00
|
|
|
|
2023-04-04 20:22:57 +02:00
|
|
|
export const timeoutWrap = async <T = any>(
|
|
|
|
promiseArg: Promise<T>,
|
|
|
|
timeoutInMsArg: number,
|
|
|
|
rejectArg = true
|
|
|
|
) => {
|
2020-10-16 02:06:50 +00:00
|
|
|
return new Promise<T>((resolve, reject) => {
|
2020-10-15 18:02:14 +00:00
|
|
|
setTimeout(() => {
|
2022-02-27 21:02:27 +01:00
|
|
|
if (rejectArg) {
|
|
|
|
reject(new Error('timeout'));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}, timeoutInMsArg);
|
2020-10-16 01:39:19 +00:00
|
|
|
promiseArg.then(resolve, reject);
|
2020-10-15 18:02:14 +00:00
|
|
|
});
|
|
|
|
};
|
2021-04-23 17:58:37 +00:00
|
|
|
|
2023-04-04 20:22:57 +02:00
|
|
|
export const timeoutAndContinue = async <T = any>(
|
|
|
|
promiseArg: Promise<T>,
|
|
|
|
timeoutInMsArg = 60000
|
|
|
|
) => {
|
2022-02-27 21:02:27 +01:00
|
|
|
return timeoutWrap(promiseArg, timeoutInMsArg, false);
|
2023-04-04 20:22:57 +02:00
|
|
|
};
|
2022-02-27 21:02:27 +01:00
|
|
|
|
2021-04-23 17:58:37 +00:00
|
|
|
export const getFirstTrueOrFalse = async (promisesArg: Promise<boolean>[]) => {
|
2021-04-23 18:04:08 +00:00
|
|
|
const done = defer<boolean>();
|
2021-04-23 17:58:37 +00:00
|
|
|
for (const promiseArg of promisesArg) {
|
|
|
|
promiseArg.then((resultArg) => {
|
|
|
|
if (resultArg === true) {
|
|
|
|
done.resolve(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Promise.all(promisesArg).then(() => {
|
|
|
|
done.resolve(false);
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
2025-01-07 04:27:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a Node.js-style callback-based function into a Promise.
|
|
|
|
* @param fn The function that expects a callback.
|
|
|
|
* @returns A Promise that resolves with the result of the function or rejects with an error.
|
|
|
|
*/
|
|
|
|
export const fromCallback = <T>(
|
|
|
|
fn: (callback: (err: NodeJS.ErrnoException | null, result?: T) => void) => void
|
|
|
|
): Promise<T> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fn((err, result) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err); // Reject the promise with the error
|
|
|
|
} else {
|
|
|
|
resolve(result as T); // Resolve the promise with the result
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|