概述
- 我有一个
f1
非异步函数。 f1
被多次调用,我无法控制调用f1
- 当 f1 被调用时,我想调用一个异步函数
f2
目标:
- 我想
f2
在下一次f2
执行之前完成
问题:
- 如何确保
f2
按顺序执行?(下面的示例代码)
//I have no control over f1
//f1 can get called multiple times in quick succession.
func f1() {
Task {
try await f2() // Next time f1 gets called it should wait for the previous f2 to complete then should execute f2
}
}
func f2() async throws {}