最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

swift - Running 3 functions serially each running when the previous function completes - Stack Overflow

matteradmin2PV0评论

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 on the try await [one, two, three] line:

  1. No calls to throwing functions occur within try expression
  2. No async operations occur within await expression
  3. Expression of type [()] is unused.

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.

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 on the try await [one, two, three] line:

  1. No calls to throwing functions occur within try expression
  2. No async operations occur within await expression
  3. Expression of type [()] is unused.

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.

Share Improve this question edited Nov 16, 2024 at 18:44 Alexander 63.5k13 gold badges105 silver badges168 bronze badges asked Nov 16, 2024 at 2:55 user1233894user1233894 1,7761 gold badge12 silver badges11 bronze badges 1
  • These 3 functions are suspicious: if it's important that they run in a specific order, why is it that none of them take input, or return a result? Most people would expect to see something like let one = await func1(), let two = await func2(one), let three = await func3(two). That would make the dependency obvious, and impossible to get wrong. – Alexander Commented Nov 16, 2024 at 18:46
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Post a comment

comment list (0)

  1. No comments so far