swiftasync-awaitconcurrency

Running 3 functions serially each running when the previous function completes


I'm trying to run 3 functions serially having each function complete before the next one starts. The below code works fine but I get three warnings 1) No calls to throwing functions occur within 'try' expression 2) No 'async' operations occur within 'await' expression 3) Expression of type '[()]' is unused. The warnings show on this line "try await [one, two, three]"

Any suggestions on the proper way to code this and not get these warnings?

Task {
        let one: () = await func1()
        let two: ()  = await func2()
        let three: ()  = await func3()
        try await [one, two, three]
      }

 func func1() async {
...
 }

func func2() async {
 ...
 }

func func3() async {
 ...
 }

The functions essentially decode JSON data from separate external sources and then calculations are done on the results so its important each decoding happens before the next one starts


Solution

  • It is simply:

    Task {
        await func1()
        await func2()
        await func3()
    }
    

    That will run them consecutively. Just be wary of introducing additional unstructured concurrency (Task {…}) in those functions.