Bezalel P. answered 03/20/26
Self Learner teaching others how to learn and solve their problem
The problem is that your current implementation returns Future[List[Future[Option[DiagnosisCode]]]]. You're mapping inside a Future and then mapping again, creating nested futures.
To get Future[List[DiagnosisCode]] , use flatMap and Future.sequence to flatten (removes nested behavior).
Here's what each part does:
- flatMap instead of map on the outer Future — so you can return another Future from inside without nesting
- Future.sequence — converts List[Future[Option[DiagnosisCode]]] into Future[List[Option[DiagnosisCode]]], running all the lookups
- .flatten — collapses List[Option[DiagnosisCode]] into List[DiagnosisCode], dropping any None results (codes not found)