FoundKey/packages/backend/src/prelude/await-all.ts
Johann150 6ce4b3fe2f
Some checks failed
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/test Pipeline failed
fix some lints
Many of these were fixed automatically with eslint --fix.

Some of them (e.g. adding return types to functions) were done manually.
2022-08-11 00:09:29 +02:00

21 lines
547 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;
}