Bird
Raised Fist0
Swiftprogramming~10 mins

Await for calling async functions in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the async function and wait for its result.

Swift
func fetchData() async -> String {
    return "Data loaded"
}

func load() async {
    let result = [1] fetchData()
    print(result)
}
Drag options to blanks, or click blank then click option'
Aawait
Basync
Cdefer
Dtry
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' before the function call.
Forgetting to use any keyword and calling the function directly.
2fill in blank
medium

Complete the code to define an async function that waits for another async function.

Swift
func getNumber() async -> Int {
    return 42
}

func printNumber() async {
    let number = [1] getNumber()
    print("Number is \(number)")
}
Drag options to blanks, or click blank then click option'
Atry
Basync
Cdefer
Dawait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' instead of 'await' before the function call.
Calling the async function without 'await' causing errors.
3fill in blank
hard

Fix the error in calling the async function by adding the missing keyword.

Swift
func fetchMessage() async -> String {
    return "Hello!"
}

func showMessage() async {
    let message = [1] fetchMessage()
    print(message)
}
Drag options to blanks, or click blank then click option'
Aawait
Basync
Cdefer
Dtry
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'await' causing compile errors.
Using 'async' incorrectly before the function call.
4fill in blank
hard

Fill both blanks to create an async function that calls another async function and prints its result.

Swift
func getGreeting() [1] -> String {
    return "Hi there!"
}

func greet() [2] {
    let greeting = await getGreeting()
    print(greeting)
}
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cthrows
Ddefer
Attempts:
3 left
💡 Hint
Common Mistakes
Marking only one function as async causing errors.
Using 'await' in the function signature instead of 'async'.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that awaits async calls and filters results.

Swift
func fetchValue(_ key: String) async -> Int {
    return key.count
}

func buildDict(keys: [String]) async -> [String: Int] {
    var dict = [String: Int]()
    for key in keys {
        let value = [1] fetchValue(key)
        if value [2] 3 {
            dict[[3]] = value
        }
    }
    return dict
}
Drag options to blanks, or click blank then click option'
Aawait
B>
Ckey
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting 'await' before the async call.
Using wrong comparison operator or variable name.

Practice

(1/5)
1. What does the await keyword do when calling an async function in Swift?
easy
A. It makes the function run faster by skipping some steps.
B. It pauses the current code until the async function finishes and returns a result.
C. It converts the async function into a synchronous one automatically.
D. It runs the async function in the background without waiting.

Solution

  1. Step 1: Understand the role of await

    The await keyword tells Swift to pause the current code execution until the async function completes.
  2. Step 2: Recognize the effect on program flow

    This means the program waits for the async task to finish and then continues with the result.
  3. Final Answer:

    It pauses the current code until the async function finishes and returns a result. -> Option B
  4. Quick Check:

    await pauses code = A [OK]
Hint: Remember: await waits for async tasks to finish before continuing [OK]
Common Mistakes:
  • Thinking await makes code run faster
  • Believing await runs code in background without waiting
  • Confusing await with automatic sync conversion
2. Which of the following is the correct way to call an async function named fetchData() inside another async function in Swift?
easy
A. let result = fetchData()
B. let result = async fetchData()
C. await let result = fetchData()
D. let result = await fetchData()

Solution

  1. Step 1: Recall async function call syntax

    When calling an async function, you must use await before the function call to wait for its result.
  2. Step 2: Check the correct placement of await

    The correct syntax is let result = await fetchData(). The await keyword comes before the function call.
  3. Final Answer:

    let result = await fetchData() -> Option D
  4. Quick Check:

    Use await before async call = D [OK]
Hint: Put await right before the async function call [OK]
Common Mistakes:
  • Omitting await before the async call
  • Placing await after the assignment
  • Using async keyword instead of await
3. What will be printed by the following Swift code?
func fetchNumber() async -> Int {
    return 42
}

func printNumber() async {
    let number = await fetchNumber()
    print(number)
}

Task {
    await printNumber()
}
medium
A. No output, code will not run
B. Optional(42)
C. 42
D. Compilation error due to missing await

Solution

  1. Step 1: Understand async function behavior

    The function fetchNumber() returns 42 asynchronously. Using await waits for this value.
  2. Step 2: Trace the print statement

    The printNumber() function awaits fetchNumber() and prints the returned integer 42.
  3. Final Answer:

    42 -> Option C
  4. Quick Check:

    Await returns 42 printed = A [OK]
Hint: Await returns the actual value, so print shows 42 [OK]
Common Mistakes:
  • Expecting Optional(42) instead of 42
  • Thinking code won't run without main async
  • Missing await causing compile error
4. Identify the error in this Swift code snippet:
func getData() async -> String {
    return "Hello"
}

func showData() {
    let message = await getData()
    print(message)
}
medium
A. showData() must be marked async to use await inside it
B. Missing await keyword before getData()
C. getData() should not be async if awaited
D. print(message) cannot print async results

Solution

  1. Step 1: Check where await is used

    The await keyword is used inside showData(), but this function is not marked async.
  2. Step 2: Understand async function rules

    In Swift, you can only use await inside an async function or a Task closure. So showData() must be declared async.
  3. Final Answer:

    showData() must be marked async to use await inside it -> Option A
  4. Quick Check:

    Await requires async function = B [OK]
Hint: Use await only inside async functions or Task closures [OK]
Common Mistakes:
  • Forgetting to mark function async when using await
  • Thinking await can be used in normal functions
  • Confusing async function declaration with await usage
5. You want to fetch user data and then fetch user posts, both are async functions: fetchUser() and fetchPosts(userId:). How do you correctly wait for both results in sequence inside an async function?
hard
A. let user = await fetchUser() let posts = await fetchPosts(userId: user.id)
B. let user = fetchUser() let posts = fetchPosts(userId: user.id)
C. await let user = fetchUser() await let posts = fetchPosts(userId: user.id)
D. let user = await fetchUser() let posts = fetchPosts(userId: await user.id)

Solution

  1. Step 1: Await the first async call to get user

    You must use await before fetchUser() to get the user data before using it.
  2. Step 2: Await the second async call using user.id

    After getting the user, call fetchPosts(userId: user.id) with await to get posts.
  3. Final Answer:

    let user = await fetchUser() let posts = await fetchPosts(userId: user.id) -> Option A
  4. Quick Check:

    Await each async call in order = C [OK]
Hint: Await each async call separately in sequence [OK]
Common Mistakes:
  • Not awaiting the second async call
  • Trying to await variable declarations incorrectly
  • Using await inside function arguments wrongly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard