Member-only story
Swift async/await feature!
Are you tired of dealing with callback hell in your Swift code? Are you sick of trying to decipher nested blocks of code that seem to go on forever?
Are you tired of dealing with callback hell in your Swift code? Are you sick of trying to decipher nested blocks of code that seem to go on forever? Fear not, dear reader, because the Swift gods have heard our cries and have gifted us with the amazing async/await feature!
Async/await is like the cherry on top of your Swift sundae. It allows you to write asynchronous code in a synchronous-like style, making it easier to read and understand. No more callback hell — just pure, unadulterated bliss.
Using async/await is super easy. Just add the “async” keyword to your function and use the “await” keyword to pause execution until a task is complete.
Let’s look at an example that you must have somewhere in your codebase and you want to update it to be magically async/await.
class Example {
// This can be like a call to network to download an image
func downloadData(_ completion: @escaping (Result<String, Error>) -> Void) {
DispatchQueue.global().async {
completion(.success("My string value"))
// or…