2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
export type Promiseable<T> = {
|
|
|
|
[K in keyof T]: Promise<T[K]> | T[K];
|
2019-04-23 15:35:26 +02:00
|
|
|
};
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
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[];
|
2019-04-23 20:26:02 +02:00
|
|
|
|
|
|
|
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
|
2023-07-19 04:27:50 +02:00
|
|
|
: awaitAll(value),
|
2019-04-23 20:26:02 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|