2019-04-23 15:35:26 +02:00
|
|
|
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);
|
2019-04-23 20:26:02 +02:00
|
|
|
const values = Object.values(obj);
|
|
|
|
|
|
|
|
const resolvedValues = await Promise.all(values.map(value =>
|
2019-04-24 01:11:19 +02:00
|
|
|
(!value || !value.constructor || value.constructor.name !== 'Object')
|
2019-04-23 20:26:02 +02:00
|
|
|
? value
|
|
|
|
: awaitAll(value)
|
|
|
|
));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|