2022-01-18 06:27:10 -07:00
|
|
|
export type Promiseable<T> = {
|
|
|
|
[K in keyof T]: Promise<T[K]> | T[K];
|
2019-04-23 07:35:26 -06:00
|
|
|
};
|
|
|
|
|
2022-01-18 06:27:10 -07: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 12:26:02 -06:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const resolvedValues = await Promise.all(
|
|
|
|
values.map((value) =>
|
2023-01-16 12:19:20 -07:00
|
|
|
!value?.constructor || value.constructor.name !== "Object"
|
2023-01-12 21:40:33 -07:00
|
|
|
? value
|
|
|
|
: awaitAll(value),
|
|
|
|
),
|
|
|
|
);
|
2019-04-23 12:26:02 -06:00
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
target[keys[i]] = resolvedValues[i];
|
2019-04-23 07:35:26 -06:00
|
|
|
}
|
2019-04-23 12:26:02 -06:00
|
|
|
|
2019-04-23 07:35:26 -06:00
|
|
|
return target;
|
|
|
|
}
|