1 import util from
'node:util';
3 const isUndefined
= (value
: unknown
): boolean => {
4 return typeof value
=== 'undefined';
7 export const ifUndefined
= <T
>(value
: T
| undefined, isValue
: T
): T
=> {
8 if (isUndefined(value
) === true) return isValue
;
12 // const isIterable = <T>(obj: T): boolean => {
13 // if (obj === null || obj === undefined) {
16 // return typeof (obj as unknown as Iterable<T>)[Symbol.iterator] === 'function';
19 // const ifNotIterableDo = <T>(obj: T, cb: () => void): void => {
20 // if (isIterable(obj) === false) cb();
23 const isPromisePending
= (promise
: Promise
<unknown
>): boolean => {
24 return util
.inspect(promise
).includes('pending');
27 export const promiseWithTimeout
= <T
>(
31 timeoutCallback
: () => void = () => {
32 /* This is intentional */
35 // Create a timeout promise that rejects in timeout milliseconds
36 const timeoutPromise
= new Promise
<never>((_
, reject
) => {
38 if (isPromisePending(promise
)) {
40 // FIXME: The original promise shall be canceled
46 // Returns a race between timeout promise and the passed promise
47 return Promise
.race
<T
>([promise
, timeoutPromise
]);
50 // export const compose = <T>(...fns: ((arg: T) => T)[]): ((x: T) => T) => {
51 // return (x: T) => fns.reduceRight((y, fn) => fn(y), x);