1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-25 03:58:27 +09:00
misskey/src/prelude/await-all.ts

24 lines
543 B
TypeScript
Raw Normal View History

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-24 03:26:02 +09:00
const values = Object.values(obj);
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];
}
2019-04-24 03:26:02 +09:00
return target;
}