Как преобразовать следующий код в асинхронный?
let m1 x = x * 2
let m2 s =
let l = [1..10]
l |> List.iter(fun x -> printfn "%d" (m1 x))
s // the function returns something
Следующее сообщение получит ошибку.
let n1 x = async { return x * 2 }
let n2 s =
async {
let l = [1..10] // l will be generated by s
l |> List.iter(fun x ->
let y = do! n1 x // ERROR: This construct may only be used within computation expressions
printfn "%d" y)
return s
}
Следующее избавит от ошибки, но вызовет n1
синхронно.
let n2 s =
async {
let l = [1..10]
l |> List.iter(fun x ->
async {
let! y = n1 x
printfn "%d" y
} |> Async.RunSynchronously)
return s
}
n2
будет вызываться в List.iter
в другой функции.Итак, на внешнем уровне я хочу сделать Async.Parallel
для списка.