How do I use the new Swift 5.5 await
keyword to wait for a duration of time?
Normally, with completion handlers, you would have something like this by using DispatchQueue
's asyncAfter(deadline:execute:)
:
func someLongTask(completion: @escaping (Int) -> Void) {
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
completion(Int.random(in: 1 ... 6))
}
}
someLongTask { diceRoll in
print(diceRoll)
}
How can this be converted to using async
& await
in Swift 5.5?