F # wait for async methods in list.iteri

I have the following code in F # 4.0

let processEscalation escalationAction (escalationEvents:UpdateCmd.Record list) =
    printf "%A" Environment.NewLine
    printf "Started %A" escalationAction
    escalationEvents
    |> List.iter ( fun x -> 
        printf "%A" Environment.NewLine
        printf "escalation %A for with action: %A" x.incident_id escalationAction
        service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated")) 
        |> Async.AwaitTask
        |> ignore)


let ComposeEscalation() = 
    let escalationlevels = ["ESC1 REACHED"; "ESC2 REACHED"; "ESC3 REACHED"]
    escalationlevels 
    |> List.map getEscalationEvents
    |> List.iteri (fun i x -> processEscalation escalationlevels.[i] x)

where the next line is a call to the async C # method that returns Task

service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))

The constituent escalation method calls the process_scalation procedure three times. However, the second call begins before the completion of the first call. How can I make sure the last line, list.iteri waits and processes them sequentially? Perhaps the Escalation process should be in an async calculation expression?

+4
source share
1 answer

Async.AwaitTask, , Async, . , .

- :

service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated")) 
|> Async.AwaitTask
|> Async.RunSynchronously
|> ignore

, , , , .

: , , - , Async.Parallel:

module Async = 

    let sequential (asyncs: seq<Async<'t>>) : Async<'t []> = 
        let rec loop acc remaining = 
            async {
                match remaining with
                | [] -> return Array.ofList (List.rev acc)
                | x::xs ->
                    let! res = x
                    return! loop (res::acc) xs
            }
        loop [] (List.ofSeq asyncs)

- :

escalationEvents
// a collection of asyncs - note that the task won't start until the async is ran
|> List.map (fun x -> 
    async {
        let task = 
            service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated")) 
        return! Async.AwaitTask task 
    })
// go from a collection of asyncs into an async of a collection
|> Async.sequential
// you don't care about the result, so ignore it
|> Async.Ignore
// now that you have your async, you need to run it in a way that makes sense
// in your context - Async.Start could be another option. 
|> Async.RunSynchronously

, , , . (, , ).

+4

Source: https://habr.com/ru/post/1665313/


All Articles