코드
import Foundation
import UIKit
func abc(query: String) {
fetch(query: query, completion: {
print("abc")
})
}
func fetch(query: String, completion: (() -> ())) {
print("fetch")
}
실행결과
fetch
이유
print("abc") 같은 경우 fetch 메소드의 트레일링 클로저 내부에 있는데 completion()이 fetch에서 완료가 되지 않았기에 해당 부분이 실행이 되지 않는다.
fetch 함수 내부에서 completion()을 완료시키면 해당 부분이 실행이 된다.
아래와 같이
func fetch(query: String, completion: (() -> ())) {
print("fetch")
completion()
}
로 코드를 바꾸면 abc가 실행이 된다.