Race Named Promises and Know Which Won

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

The problem

  • hard
  • Code
  • Pro

Write raceLabeled(promises), which takes an object of named promises and settles like the first of them to settle: it resolves with { key, value }, the winner's name and value, or rejects with its error. Type it so the result is a union with one case per name, each pairing that name with its own promise's value type, so checking key narrows value; values that aren't promises are rejected.

Your code must pass

  • Resolves with the first promise's key and value
  • Rejects when the first to settle rejects
  • Works with promises that have already resolved
  • Has no type errors
  • Pairs each key with its own value type
  • Narrows value by key
  • Rejects values that aren't promises