Sobes.tech
Junior — Senior

Determining the order of output in asynchronous calls in the main queue

livecode

Task condition

This task provides an example of Swift code where output to the console occurs both synchronously and asynchronously using DispatchQueue.main.async. It is necessary to understand which numbers will be printed and in what order when the program is run.

func executeFirstTask() {
    print(5)

    DispatchQueue.main.async {
        print(10)
    }

    print(15)

    DispatchQueue.main.async {
        print(20)
    }

    print(25)
}

func executeSecondTask() {
    print(30)
}

// Main Thread
executeFirstTask()
executeSecondTask()