Swift

[Swift] async await 쓰는 법

insub4067 2022. 11. 15. 23:37

1. 함수명 뒤에 async 를 붙일것

2. Task 안에서 실행할것 (Task 밖에 두면 순차적으로 실행이 되지 않는다)

3. 호출할때 앞에 await 키워드 붙일것

func executeFirst() async {
    for i in 0...5 {
        print("first")
    }
}

func afterFirst() async {
    print("second")
}

Task {
    await executeFirst()
    await afterFirst()
    print("DONE")
}

// 결괏값👇👇
// first
// first
// first
// first
// first
// first
// second
// DONE