MediumPro challengeJavaScriptTypeScript

Awaited — Deep Promise Unwrapping

TypeScriptAsyncTypes

TypeScript 4.5 introduced the built-in Awaited<T> type that recursively unwraps nested Promises:

type Awaited<T> =
  T extends null | undefined ? T
  : T extends object & { then(f: infer F): unknown }
    ? F extends (value: infer V) => unknown
      ? Awaited<V>     // ← recursive!
      : never
    : T;

type A = Awaited<Promise<Promise<string>>>;
// A = string  (unwrapped through 2 levels)

Your Task

deepAwait(val)

Recursively await a value through multiple layers of Promises:

await deepAwait(Promise.resolve(Promise.resolve(42))) // → 42
await deepAwait('hello')                              // → 'hello'

allSettledMap(record)

Like Promise.allSettled but for an object — return each key mapped to a settled result:

await allSettledMap({
  a: Promise.resolve(1),
  b: Promise.reject('oops'),
})
// → { a: { status: 'fulfilled', value: 1 }, b: { status: 'rejected', reason: 'oops' } }

Sample tests

Test #1plain number passes through
Input: ["deepAwait",42]
Output: 42
Test #2plain string passes through
Input: ["deepAwait","plain"]
Output: "plain"
Test #3mixed fulfilled and rejected
Input: ["allSettledMap",{"a":1,"b":"oops"},["b"]]
Output: {"a":{"value":1,"status":"fulfilled"},"b":{"reason":"oops","status":"rejected"}}