Created
November 18, 2017 17:38
-
-
Save arashkashi/f2c921c788a798e0ec48e8aed29747d2 to your computer and use it in GitHub Desktop.
ConcatinateAsyncFuncs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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