mirror of
https://codeberg.org/yeentown/barkey
synced 2024-12-03 04:37:28 +00:00
0463c6bb0f
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update description.ts * wip
22 lines
620 B
TypeScript
22 lines
620 B
TypeScript
type Await<T> = T extends Promise<infer U> ? U : T;
|
|
|
|
type AwaitAll<T> = {
|
|
[P in keyof T]: Await<T[P]>;
|
|
};
|
|
|
|
export async function awaitAll<T>(obj: T): Promise<AwaitAll<T>> {
|
|
const target = {} as any;
|
|
const keys = Object.keys(obj);
|
|
const rawValues = Object.values(obj);
|
|
const retValues = ((values: any[]): any[] =>
|
|
values.map(value => {
|
|
if (!value || !value.constructor || value.constructor.name !== 'Object') return value;
|
|
return awaitAll(value);
|
|
})
|
|
)(rawValues);
|
|
const values = await Promise.all(retValues);
|
|
for (let i = 0; i < values.length; i++) {
|
|
target[keys[i]] = values[i];
|
|
}
|
|
return target;
|
|
}
|