Как создать список асинхронных c вычисленных результатов без блокировки?
У меня есть следующий код:
async {
let! result1 = requestId |> createTopicAsync "Topic.courier-eta"
let! result2 = requestId |> createTopicAsync "Topic.customer-cancelled"
let! result3 = requestId |> createTopicAsync "Topic.courier-cancelled"
let! result4 = requestId |> createTopicAsync "Topic.courier-arrived-at-pickup"
let! result6 = requestId |> createTopicAsync "Topic.courier-enroute-to-dropoff"
let! result7 = requestId |> createTopicAsync "Topic.courier-arrived-at-dropoff"
let! result8 = requestId |> createTopicAsync "Topic.courier-dropped-off"
let! result9 = requestId |> createTopicAsync "Topic.courier-arrived-at-pickup"
let errorOccurred = // I want to avoid this...
[result1
result2
result3
result4
result6
result7
result8
result9
] |> List.forall(fun x -> x |> function | Error _ -> false | Ok _ -> true)
return
if not errorOccurred
then Ok ()
else Error "Failed to create all topics"
}
Вот Обходной путь, который блокирует:
async {
return
[requestId |> createTopicAsync "Topic.courier-eta" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.customer-cancelled" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-cancelled" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-arrived-at-pickup" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-enroute-to-dropoff" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-arrived-at-dropoff" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-dropped-off" |> Async.RunSynchronously
requestId |> createTopicAsync "Topic.courier-arrived-at-pickup" |> Async.RunSynchronously
] |> List.forall(fun x -> x |> function | Error _ -> false | Ok _ -> true)
|> function
| false -> Error "Failed to create all topics"
| true -> Ok ()
}