diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2019-04-23 22:35:26 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-23 22:35:26 +0900 |
| commit | 0463c6bb0f8fd32740ceb61ccce04c662272a618 (patch) | |
| tree | a28cbdf6c9cdc14648b8c0e46248665a3ad7e5af /src/prelude | |
| parent | Fix #4768 (diff) | |
| download | misskey-0463c6bb0f8fd32740ceb61ccce04c662272a618.tar.gz misskey-0463c6bb0f8fd32740ceb61ccce04c662272a618.tar.bz2 misskey-0463c6bb0f8fd32740ceb61ccce04c662272a618.zip | |
Refactor API (#4770)
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* Update description.ts
* wip
Diffstat (limited to 'src/prelude')
| -rw-r--r-- | src/prelude/await-all.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/prelude/await-all.ts b/src/prelude/await-all.ts new file mode 100644 index 0000000000..66303fe3ba --- /dev/null +++ b/src/prelude/await-all.ts @@ -0,0 +1,22 @@ +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); + const rawValues = Object.values(obj); + const retValues = ((values: any[]): any[] => + values.map(value => { + if (!value || !value.constructor || value.constructor.name !== 'Object') return value; + return awaitAll(value); + }) + )(rawValues); + const values = await Promise.all(retValues); + for (let i = 0; i < values.length; i++) { + target[keys[i]] = values[i]; + } + return target; +} |