Skip to content

Instantly share code, notes, and snippets.

@arashkashi
Created November 18, 2017 17:38
Show Gist options
  • Save arashkashi/f2c921c788a798e0ec48e8aed29747d2 to your computer and use it in GitHub Desktop.
Save arashkashi/f2c921c788a798e0ec48e8aed29747d2 to your computer and use it in GitHub Desktop.
ConcatinateAsyncFuncs
infix operator --> :AdditionPrecedence
func --><InputA, CompA, CompB>(first: @escaping ( InputA, @escaping (CompA?) -> () ) -> (),
second: @escaping ( CompA, @escaping (CompB?) -> () ) -> ()) -> (InputA, @escaping (CompB?) -> ()) -> () {
return { (input: InputA, compB: @escaping (CompB?) -> () ) in
first(input, { compA in
guard let validCompaA = compA else { compB(nil); return }
second(validCompaA, compB)
})
}
}
func a(a: Int, completion: @escaping (Int?) -> ()) {
completion(a + 1)
}
func b(b: Int, completion: @escaping (String?) -> ()) {
completion("ahmad")
}
func c(c: String, completion: @escaping (Int?) -> ()) {
completion(66)
}
let result = a --> b --> c
result(4, { result in
if result == nil {
print("fail")
} else {
print("success")
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment