mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-10 23:03:08 +02:00
21 lines
546 B
TypeScript
21 lines
546 B
TypeScript
export type Promiseable<T> = {
|
|
[K in keyof T]: Promise<T[K]> | T[K];
|
|
};
|
|
|
|
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[];
|
|
|
|
const resolvedValues = await Promise.all(values.map(value =>
|
|
(!value || !value.constructor || value.constructor.name !== 'Object')
|
|
? value
|
|
: awaitAll(value)
|
|
));
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
target[keys[i]] = resolvedValues[i];
|
|
}
|
|
|
|
return target;
|
|
}
|