How can the following code be modified to print 2 after 1, without modifying the foo1() function?
func foo1() {
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
print("1")
}
}
func foo2() {
print("2")
}
func mainFunc() {
foo1()
foo2()
}
mainFunc()
Edit: since I was asked I prefer to specify directly in the question. This is not a real problem, it's an exercise I set myself to improve the understanding of the language. I know there are alternative ways like using completion handlers (just to name one).
The answer to this question doesn't have to exist or be yes, it might just not be possible to take a function that launches a task and externally force its behavior synchronously.
Anyway this post may be helpful for someone who is planning to study concurrency in Swift