- Async stands for asynchronous (obviously) and can be seen as a method attribute makit it clear that a method or action performs asynchronous work. (e.g.,
func fetchData() async throws -> [UserData] {}). Since this function also has thethrowattribute, it means that in case something went wrong, it throws an error which then you can manipulate in your application however you'd like. - Await is the keyword used for calling async methods. You can't have Async without Await in Swift, because they're soulmates (e.g.,
do { let data = try await fetchData() ...} catch { ... }). In this example, you can see that we useawaitto make sure that the data is first being fetched by ourfetchData()function and only then continues with the rest of its code after the output has arrived. - Async actually has another use, not only functions, but it can also declare a constant asynchronously (e.g.,
async let roadmapData = fetchRoadmapData()). This is useful, for example, if you want to fetch multiple data within the same task, but not one at a time, but all of them at the same time, and a perfect example for this is loading profile data on navigolearn. It would be odd for when you go to your profile to see first your image, and then your profile picture, then your name and so on and so forth. Just like the example with video games loading screen, the same concept applies here.