DevPrep

All topics › What does it print? Interview puzzles

global().async + main.async: what order does it print?

Middle PRO

Task

The code runs on the app's main thread.

func run() {
    print("2")
    DispatchQueue.global().async {
        print("3")
        DispatchQueue.main.async {
            print("4")
        }
        print("5")
    }
    print("6")
}

print("1")
run()
print("7")

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store