From 701ab6cc6826a2e272d614fae97e7058110f1e1f Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 24 Apr 2019 03:26:02 +0900 Subject: [PATCH] Refactor --- src/prelude/await-all.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/prelude/await-all.ts b/src/prelude/await-all.ts index 66303fe3b..949255048 100644 --- a/src/prelude/await-all.ts +++ b/src/prelude/await-all.ts @@ -7,16 +7,17 @@ type AwaitAll = { export async function awaitAll(obj: T): Promise> { 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]; + 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]; } + return target; }