A completion handler in Swift is a function that calls back when a task completes. This is why it is also called a callback function.
A callback function is passed as an argument into another function. When this function completes running a task, it executes the callback function.
Example :
Here is a demonstrative (but not useful) example of using a completion handler in Swift:
func greet(_ completion: () -> ()) {
print("Hello world!")
completion()
}
greet({
print("Task finished.")
})
Output :
Hello world!
Task finished.