Await an Object of Promises by Key

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code
  • Pro

Promise.all takes a list, but data for a page is often an object: { user: fetchUser(), posts: fetchPosts() }. Export a type Resolved<T> that gives each property of T its awaited type, and write resolveAll(obj): wait for every value of obj at once (plain values are allowed too) and resolve to an object with the same keys, in the same order, holding the results, or reject as soon as any of them rejects. Type the result as a promise of Resolved of its input, so reading a key it doesn't have is a type error.

Your code must pass

  • Resolves each key to its value
  • Rejects if any promise rejects
  • Resolves an empty object to an empty object
  • Has no type errors
  • Resolves each key's value type
  • Keeps exactly the same keys