2022-01-18 14:27:10 +01:00
|
|
|
export type Promiseable<T> = {
|
|
|
|
[K in keyof T]: Promise<T[K]> | T[K];
|
2019-04-23 15:35:26 +02:00
|
|
|
};
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
export async function awaitAll<T>(obj: Promiseable<T>): Promise<T> {
|
|
|
|
const target = {} as T;
|
|
|
|
const keys = Object.keys(obj) as unknown as (keyof T)[];
|
|
|
|
const values = Object.values(obj) as any[];
|
2019-04-23 20:26:02 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const resolvedValues = await Promise.all(
|
|
|
|
values.map((value) =>
|
2023-01-16 20:19:20 +01:00
|
|
|
!value?.constructor || value.constructor.name !== "Object"
|
2023-01-13 05:40:33 +01:00
|
|
|
? value
|
|
|
|
: awaitAll(value),
|
|
|
|
),
|
|
|
|
);
|
2019-04-23 20:26:02 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
target[keys[i]] = resolvedValues[i];
|
2019-04-23 15:35:26 +02:00
|
|
|
}
|
2019-04-23 20:26:02 +02:00
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
return target;
|
|
|
|
}
|