Refactor
This commit is contained in:
parent
4e90fd3a06
commit
701ab6cc68
|
@ -7,16 +7,17 @@ type AwaitAll<T> = {
|
||||||
export async function awaitAll<T>(obj: T): Promise<AwaitAll<T>> {
|
export async function awaitAll<T>(obj: T): Promise<AwaitAll<T>> {
|
||||||
const target = {} as any;
|
const target = {} as any;
|
||||||
const keys = Object.keys(obj);
|
const keys = Object.keys(obj);
|
||||||
const rawValues = Object.values(obj);
|
const values = Object.values(obj);
|
||||||
const retValues = ((values: any[]): any[] =>
|
|
||||||
values.map(value => {
|
const resolvedValues = await Promise.all(values.map(value =>
|
||||||
if (!value || !value.constructor || value.constructor.name !== 'Object') return value;
|
(value || !value.constructor || value.constructor.name !== 'Object')
|
||||||
return awaitAll(value);
|
? value
|
||||||
})
|
: awaitAll(value)
|
||||||
)(rawValues);
|
));
|
||||||
const values = await Promise.all(retValues);
|
|
||||||
for (let i = 0; i < values.length; i++) {
|
for (let i = 0; i < keys.length; i++) {
|
||||||
target[keys[i]] = values[i];
|
target[keys[i]] = resolvedValues[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue