Let's say i have few file formats i can read from. eg. xml, yaml and json. For each format i have specific reader but i do not know until runtime which file was placed so i try to read file with all of these readers. This whole process is currently async.
All i want is a proper way to return only 'FileReaderTask' which completed with result.
So far i came with this but i don't like it too much. Especially the part where i return null
public async Task<ReadResponse> Read(string id)
{
var readerDetails = _readerDetailsRepository.GetAll();
var tasks = readerDetails.Select(readerDetail => _reader.ReadAsync(readerDetail, id)).ToList();
foreach (var result in await Task.WhenAll(tasks))
{
if (!string.IsNullOrEmpty(result)) // only one will have the response !
{
return await HandleResponse(_jsonConverter.Convert<ReadResponse>(result), id);
}
}
return null; // ?? seems pretty bad
}