Skip to content

Instantly share code, notes, and snippets.

@JarvisTheAvenger
Created August 7, 2021 05:05
Show Gist options
  • Save JarvisTheAvenger/5d8540eab3165b4f7e9e1d5c62159d2b to your computer and use it in GitHub Desktop.
Save JarvisTheAvenger/5d8540eab3165b4f7e9e1d5c62159d2b to your computer and use it in GitHub Desktop.
import Foundation
// Closures
// Escaping closures vs Non Escaping closures
// Escaping closures escapes the function body
func doSomething(_ a: Int, completion: @escaping (Int) -> Void) {
var a = a
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
a += 10
completion(a)
}
completion(a)
}
doSomething(2) { value in
print(value)
}
func createSomething(_ a: Int, completion: ((Int) -> Void)?) {
var a = a
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
a += 10
completion?(a)
}
completion?(a)
}
// Trailing closure
createSomething(2) { value in
print(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment