We have something where we have a URI and some resolvers. We pass the URI to each resolver in turn. Each resolver handles one particular type of URI, so only one of them will return a result. So we just loop over the resolvers and break when one of them gives us a result.
But I was thinking about what might happen if we have hundreds or thousands of resolvers and we want to parallelize it. So we want to start a lot of coroutines and when one of them gives a result we cancel the others and return. Unfortunately coroutines doesn’t have an
awaitFirst(), so how would I do this?
My first thought is to create a
CoroutineScope, and then within that create a
SharedFlow and launch all of my coroutines to run the resolvers. Each coroutine would take the
SharedFlow, and if the resolver returned a value it would emit it to the flow. The flow’s collector would output the value and then cancel the scope.
Does anybody have a better idea?