0
0
Swiftprogramming~5 mins

Async functions declaration in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What keyword do you use in Swift to declare an asynchronous function?
You use the async keyword after the function's parameter list to declare it as asynchronous.
Click to reveal answer
intermediate
How do you mark a Swift function that can throw errors and is asynchronous?
You place async before throws in the function declaration, like func fetchData() async throws.
Click to reveal answer
beginner
What is the purpose of declaring a function as async in Swift?
Declaring a function as async means it can perform work that might take time without blocking the program, allowing other code to run while waiting.
Click to reveal answer
beginner
Show the syntax of a simple async function in Swift that returns an integer.
Example:<br>
func getNumber() async -> Int {
    return 42
}
Click to reveal answer
intermediate
Can an async function in Swift be called like a normal function? Why or why not?
No, async functions must be called with await because they might pause execution until the result is ready.
Click to reveal answer
Which keyword is used to declare an asynchronous function in Swift?
Aasync
Bawait
Cdefer
Dthrows
How do you call an async function in Swift?
AJust call it like a normal function
BUse the <code>await</code> keyword before the call
CUse the <code>async</code> keyword before the call
DUse the <code>try</code> keyword only
What does the async keyword indicate about a Swift function?
AIt can pause and resume later without blocking
BIt throws an error
CIt runs instantly without delay
DIt is a private function
Which of these is a correct async function declaration in Swift?
Aasync func loadData() throws -> String
Bfunc loadData() throws async -> String
Cfunc loadData() async throws -> String
Dfunc async loadData() throws -> String
What must you add before calling an async function inside another async function?
Adefer
Basync
Ctry
Dawait
Explain how to declare an async function in Swift and why you would use it.
Think about how async helps with waiting for tasks without stopping the whole program.
You got /4 concepts.
    Describe the difference between calling a normal function and an async function in Swift.
    Focus on what you add before calling an async function and why.
    You got /4 concepts.