├── README.md ├── TestAsync ├── TestAsync.xcodeproj │ ├── project.pbxproj │ └── xcuserdata │ │ └── wenjin.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ └── TestAsync.xcscheme └── TestAsync │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift └── functional async.pdf /README.md: -------------------------------------------------------------------------------- 1 | # functional_async_demo 2 | solve async problem in functional way 3 | -------------------------------------------------------------------------------- /TestAsync/TestAsync.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | BC469BC21D6DE57D00E509E8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC469BC11D6DE57D00E509E8 /* AppDelegate.swift */; }; 11 | BC469BC41D6DE57D00E509E8 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC469BC31D6DE57D00E509E8 /* ViewController.swift */; }; 12 | BC469BC71D6DE57D00E509E8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BC469BC51D6DE57D00E509E8 /* Main.storyboard */; }; 13 | BC469BC91D6DE57D00E509E8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BC469BC81D6DE57D00E509E8 /* Assets.xcassets */; }; 14 | BC469BCC1D6DE57D00E509E8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BC469BCA1D6DE57D00E509E8 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | BC469BBE1D6DE57C00E509E8 /* TestAsync.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TestAsync.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | BC469BC11D6DE57D00E509E8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | BC469BC31D6DE57D00E509E8 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | BC469BC61D6DE57D00E509E8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | BC469BC81D6DE57D00E509E8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | BC469BCB1D6DE57D00E509E8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | BC469BCD1D6DE57D00E509E8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | BC469BBB1D6DE57C00E509E8 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | BC469BB51D6DE57C00E509E8 = { 39 | isa = PBXGroup; 40 | children = ( 41 | BC469BC01D6DE57C00E509E8 /* TestAsync */, 42 | BC469BBF1D6DE57C00E509E8 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | BC469BBF1D6DE57C00E509E8 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | BC469BBE1D6DE57C00E509E8 /* TestAsync.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | BC469BC01D6DE57C00E509E8 /* TestAsync */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | BC469BC11D6DE57D00E509E8 /* AppDelegate.swift */, 58 | BC469BC31D6DE57D00E509E8 /* ViewController.swift */, 59 | BC469BC51D6DE57D00E509E8 /* Main.storyboard */, 60 | BC469BC81D6DE57D00E509E8 /* Assets.xcassets */, 61 | BC469BCA1D6DE57D00E509E8 /* LaunchScreen.storyboard */, 62 | BC469BCD1D6DE57D00E509E8 /* Info.plist */, 63 | ); 64 | path = TestAsync; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | BC469BBD1D6DE57C00E509E8 /* TestAsync */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = BC469BD01D6DE57D00E509E8 /* Build configuration list for PBXNativeTarget "TestAsync" */; 73 | buildPhases = ( 74 | BC469BBA1D6DE57C00E509E8 /* Sources */, 75 | BC469BBB1D6DE57C00E509E8 /* Frameworks */, 76 | BC469BBC1D6DE57C00E509E8 /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = TestAsync; 83 | productName = TestAsync; 84 | productReference = BC469BBE1D6DE57C00E509E8 /* TestAsync.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | BC469BB61D6DE57C00E509E8 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0730; 94 | LastUpgradeCheck = 0730; 95 | ORGANIZATIONNAME = wenjin; 96 | TargetAttributes = { 97 | BC469BBD1D6DE57C00E509E8 = { 98 | CreatedOnToolsVersion = 7.3.1; 99 | LastSwiftMigration = 0820; 100 | }; 101 | }; 102 | }; 103 | buildConfigurationList = BC469BB91D6DE57C00E509E8 /* Build configuration list for PBXProject "TestAsync" */; 104 | compatibilityVersion = "Xcode 3.2"; 105 | developmentRegion = English; 106 | hasScannedForEncodings = 0; 107 | knownRegions = ( 108 | en, 109 | Base, 110 | ); 111 | mainGroup = BC469BB51D6DE57C00E509E8; 112 | productRefGroup = BC469BBF1D6DE57C00E509E8 /* Products */; 113 | projectDirPath = ""; 114 | projectRoot = ""; 115 | targets = ( 116 | BC469BBD1D6DE57C00E509E8 /* TestAsync */, 117 | ); 118 | }; 119 | /* End PBXProject section */ 120 | 121 | /* Begin PBXResourcesBuildPhase section */ 122 | BC469BBC1D6DE57C00E509E8 /* Resources */ = { 123 | isa = PBXResourcesBuildPhase; 124 | buildActionMask = 2147483647; 125 | files = ( 126 | BC469BCC1D6DE57D00E509E8 /* LaunchScreen.storyboard in Resources */, 127 | BC469BC91D6DE57D00E509E8 /* Assets.xcassets in Resources */, 128 | BC469BC71D6DE57D00E509E8 /* Main.storyboard in Resources */, 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | /* End PBXResourcesBuildPhase section */ 133 | 134 | /* Begin PBXSourcesBuildPhase section */ 135 | BC469BBA1D6DE57C00E509E8 /* Sources */ = { 136 | isa = PBXSourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | BC469BC41D6DE57D00E509E8 /* ViewController.swift in Sources */, 140 | BC469BC21D6DE57D00E509E8 /* AppDelegate.swift in Sources */, 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXSourcesBuildPhase section */ 145 | 146 | /* Begin PBXVariantGroup section */ 147 | BC469BC51D6DE57D00E509E8 /* Main.storyboard */ = { 148 | isa = PBXVariantGroup; 149 | children = ( 150 | BC469BC61D6DE57D00E509E8 /* Base */, 151 | ); 152 | name = Main.storyboard; 153 | sourceTree = ""; 154 | }; 155 | BC469BCA1D6DE57D00E509E8 /* LaunchScreen.storyboard */ = { 156 | isa = PBXVariantGroup; 157 | children = ( 158 | BC469BCB1D6DE57D00E509E8 /* Base */, 159 | ); 160 | name = LaunchScreen.storyboard; 161 | sourceTree = ""; 162 | }; 163 | /* End PBXVariantGroup section */ 164 | 165 | /* Begin XCBuildConfiguration section */ 166 | BC469BCE1D6DE57D00E509E8 /* Debug */ = { 167 | isa = XCBuildConfiguration; 168 | buildSettings = { 169 | ALWAYS_SEARCH_USER_PATHS = NO; 170 | CLANG_ANALYZER_NONNULL = YES; 171 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 172 | CLANG_CXX_LIBRARY = "libc++"; 173 | CLANG_ENABLE_MODULES = YES; 174 | CLANG_ENABLE_OBJC_ARC = YES; 175 | CLANG_WARN_BOOL_CONVERSION = YES; 176 | CLANG_WARN_CONSTANT_CONVERSION = YES; 177 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 178 | CLANG_WARN_EMPTY_BODY = YES; 179 | CLANG_WARN_ENUM_CONVERSION = YES; 180 | CLANG_WARN_INT_CONVERSION = YES; 181 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 182 | CLANG_WARN_UNREACHABLE_CODE = YES; 183 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 184 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 185 | COPY_PHASE_STRIP = NO; 186 | DEBUG_INFORMATION_FORMAT = dwarf; 187 | ENABLE_STRICT_OBJC_MSGSEND = YES; 188 | ENABLE_TESTABILITY = YES; 189 | GCC_C_LANGUAGE_STANDARD = gnu99; 190 | GCC_DYNAMIC_NO_PIC = NO; 191 | GCC_NO_COMMON_BLOCKS = YES; 192 | GCC_OPTIMIZATION_LEVEL = 0; 193 | GCC_PREPROCESSOR_DEFINITIONS = ( 194 | "DEBUG=1", 195 | "$(inherited)", 196 | ); 197 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 198 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 199 | GCC_WARN_UNDECLARED_SELECTOR = YES; 200 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 201 | GCC_WARN_UNUSED_FUNCTION = YES; 202 | GCC_WARN_UNUSED_VARIABLE = YES; 203 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 204 | MTL_ENABLE_DEBUG_INFO = YES; 205 | ONLY_ACTIVE_ARCH = YES; 206 | SDKROOT = iphoneos; 207 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 208 | }; 209 | name = Debug; 210 | }; 211 | BC469BCF1D6DE57D00E509E8 /* Release */ = { 212 | isa = XCBuildConfiguration; 213 | buildSettings = { 214 | ALWAYS_SEARCH_USER_PATHS = NO; 215 | CLANG_ANALYZER_NONNULL = YES; 216 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 217 | CLANG_CXX_LIBRARY = "libc++"; 218 | CLANG_ENABLE_MODULES = YES; 219 | CLANG_ENABLE_OBJC_ARC = YES; 220 | CLANG_WARN_BOOL_CONVERSION = YES; 221 | CLANG_WARN_CONSTANT_CONVERSION = YES; 222 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 223 | CLANG_WARN_EMPTY_BODY = YES; 224 | CLANG_WARN_ENUM_CONVERSION = YES; 225 | CLANG_WARN_INT_CONVERSION = YES; 226 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 227 | CLANG_WARN_UNREACHABLE_CODE = YES; 228 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 229 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 230 | COPY_PHASE_STRIP = NO; 231 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 232 | ENABLE_NS_ASSERTIONS = NO; 233 | ENABLE_STRICT_OBJC_MSGSEND = YES; 234 | GCC_C_LANGUAGE_STANDARD = gnu99; 235 | GCC_NO_COMMON_BLOCKS = YES; 236 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 237 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 238 | GCC_WARN_UNDECLARED_SELECTOR = YES; 239 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 240 | GCC_WARN_UNUSED_FUNCTION = YES; 241 | GCC_WARN_UNUSED_VARIABLE = YES; 242 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 243 | MTL_ENABLE_DEBUG_INFO = NO; 244 | SDKROOT = iphoneos; 245 | VALIDATE_PRODUCT = YES; 246 | }; 247 | name = Release; 248 | }; 249 | BC469BD11D6DE57D00E509E8 /* Debug */ = { 250 | isa = XCBuildConfiguration; 251 | buildSettings = { 252 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 253 | INFOPLIST_FILE = TestAsync/Info.plist; 254 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 255 | PRODUCT_BUNDLE_IDENTIFIER = s.TestAsync; 256 | PRODUCT_NAME = "$(TARGET_NAME)"; 257 | SWIFT_VERSION = 3.0; 258 | }; 259 | name = Debug; 260 | }; 261 | BC469BD21D6DE57D00E509E8 /* Release */ = { 262 | isa = XCBuildConfiguration; 263 | buildSettings = { 264 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 265 | INFOPLIST_FILE = TestAsync/Info.plist; 266 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 267 | PRODUCT_BUNDLE_IDENTIFIER = s.TestAsync; 268 | PRODUCT_NAME = "$(TARGET_NAME)"; 269 | SWIFT_VERSION = 3.0; 270 | }; 271 | name = Release; 272 | }; 273 | /* End XCBuildConfiguration section */ 274 | 275 | /* Begin XCConfigurationList section */ 276 | BC469BB91D6DE57C00E509E8 /* Build configuration list for PBXProject "TestAsync" */ = { 277 | isa = XCConfigurationList; 278 | buildConfigurations = ( 279 | BC469BCE1D6DE57D00E509E8 /* Debug */, 280 | BC469BCF1D6DE57D00E509E8 /* Release */, 281 | ); 282 | defaultConfigurationIsVisible = 0; 283 | defaultConfigurationName = Release; 284 | }; 285 | BC469BD01D6DE57D00E509E8 /* Build configuration list for PBXNativeTarget "TestAsync" */ = { 286 | isa = XCConfigurationList; 287 | buildConfigurations = ( 288 | BC469BD11D6DE57D00E509E8 /* Debug */, 289 | BC469BD21D6DE57D00E509E8 /* Release */, 290 | ); 291 | defaultConfigurationIsVisible = 0; 292 | defaultConfigurationName = Release; 293 | }; 294 | /* End XCConfigurationList section */ 295 | }; 296 | rootObject = BC469BB61D6DE57C00E509E8 /* Project object */; 297 | } 298 | -------------------------------------------------------------------------------- /TestAsync/TestAsync.xcodeproj/xcuserdata/wenjin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /TestAsync/TestAsync.xcodeproj/xcuserdata/wenjin.xcuserdatad/xcschemes/TestAsync.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /TestAsync/TestAsync/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TestAsync 4 | // 5 | // Created by wenjin on 8/24/16. 6 | // Copyright © 2016 wenjin. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /TestAsync/TestAsync/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /TestAsync/TestAsync/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /TestAsync/TestAsync/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /TestAsync/TestAsync/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /TestAsync/TestAsync/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TestAsync 4 | // 5 | // Created by wenjin on 8/24/16. 6 | // Copyright © 2016 wenjin. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | typealias AsyncFunc = (_ info : AnyObject,_ complete:(AnyObject?,NSError?)->Void) -> Void 11 | 12 | 13 | infix operator <> {associativity left precedence 150} 14 | infix operator +> {associativity left precedence 150} 15 | 16 | 17 | func +>(left : @escaping AsyncFunc , right : @escaping AsyncFunc) -> AsyncFunc{ 18 | return { info , complete in 19 | left(info){ result,error in 20 | guard error == nil else{ 21 | complete(nil,error) 22 | return 23 | } 24 | right(info){result,error in 25 | complete(result,error) 26 | } 27 | } 28 | } 29 | } 30 | 31 | func <>(left : @escaping AsyncFunc , right : @escaping AsyncFunc) -> AsyncFunc{ 32 | return { info, complete in 33 | 34 | var leftComplete = false 35 | var rightComplete = false 36 | var finishedComplete = false 37 | 38 | var leftResult:AnyObject? = nil 39 | var rightResult:AnyObject? = nil 40 | 41 | let checkComplete = { 42 | print ("checking") 43 | if leftComplete && rightComplete{ 44 | objc_sync_enter(finishedComplete) 45 | if !finishedComplete{ 46 | let finalResult:[AnyObject] = [leftResult!, rightResult!] 47 | complete(finalResult as AnyObject?, nil) 48 | finishedComplete = true 49 | } 50 | objc_sync_exit(finishedComplete) 51 | } 52 | } 53 | 54 | left(info){result,error in 55 | guard error == nil else{ 56 | complete(nil, error) 57 | return 58 | } 59 | 60 | leftComplete = true 61 | leftResult = result; 62 | checkComplete() 63 | } 64 | 65 | right(info){result,error in 66 | guard error == nil else{ 67 | complete(nil, error) 68 | return 69 | } 70 | 71 | rightComplete = true 72 | rightResult = result; 73 | checkComplete() 74 | } 75 | } 76 | } 77 | 78 | class Promise { 79 | var chain : AsyncFunc 80 | var alwaysClosure : ((Void)->Void)? 81 | var errorClosure : ((NSError?)->Void)? 82 | 83 | init(starter : @escaping AsyncFunc){ 84 | chain = starter 85 | } 86 | 87 | func then(_ body : @escaping (AnyObject) throws->Void )->Promise{ 88 | let async: AsyncFunc = { info, complete in 89 | DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { 90 | var error : NSError? 91 | do{ 92 | try body(info) 93 | }catch let err as NSError{ 94 | error = err 95 | } 96 | complete(0 as AnyObject?,error) 97 | } 98 | } 99 | chain = chain +> async 100 | return self 101 | } 102 | 103 | func always(_ closure : @escaping (Void)->Void)->Promise{ 104 | alwaysClosure = closure 105 | return self 106 | } 107 | 108 | func error(_ closure : @escaping (NSError?)->Void)->Promise{ 109 | errorClosure = closure 110 | fire() 111 | return self 112 | } 113 | 114 | func fire(){ 115 | chain(0 as AnyObject) { (info, error) in 116 | if let always = self.alwaysClosure{ 117 | always() 118 | } 119 | 120 | if error == nil{ 121 | print("all task finished") 122 | }else{ 123 | if let errorC = self.errorClosure{ 124 | errorC(error) 125 | } 126 | } 127 | } 128 | } 129 | } 130 | 131 | func firstly(_ body : @escaping (Void)->Void)->Promise{ 132 | 133 | let starter: AsyncFunc = { _,complete in 134 | DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { 135 | body(); 136 | complete(0 as AnyObject?,nil); 137 | } 138 | } 139 | 140 | return Promise(starter: starter) 141 | } 142 | 143 | func when(_ fstBody : @escaping ((Void)->Void), sndBody : @escaping ((Void)->Void)){ 144 | let async1 : AsyncFunc = { _ , complete in 145 | DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { 146 | fstBody(); 147 | complete(0 as AnyObject?,nil); 148 | } 149 | } 150 | 151 | let async2 : AsyncFunc = { _ , complete in 152 | DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async { 153 | sndBody(); 154 | complete(0 as AnyObject?,nil); 155 | } 156 | } 157 | 158 | let async = async1 <> async2 159 | 160 | var finished = false 161 | 162 | async(0 as AnyObject) { (_, _) in 163 | finished = true 164 | } 165 | 166 | while finished == false { 167 | 168 | } 169 | } 170 | 171 | class ViewController: UIViewController { 172 | 173 | override func viewDidLoad() { 174 | super.viewDidLoad() 175 | 176 | firstly { () in 177 | when({ () in 178 | print ("begin fst job") 179 | sleep(1) 180 | print("fst job in when finished") 181 | }, sndBody: { () in 182 | print ("begin snd job") 183 | sleep(5) 184 | print("snd job in when finished") 185 | }) 186 | }.then { (info) in 187 | print("second job") 188 | throw NSError(domain: "error", code: 0, userInfo: [:]) 189 | }.then { (info) in 190 | print("third job") 191 | }.always { () in 192 | print("always block") 193 | }.error { (error) in 194 | print("error occurred") 195 | } 196 | } 197 | 198 | override func didReceiveMemoryWarning() { 199 | super.didReceiveMemoryWarning() 200 | // Dispose of any resources that can be recreated. 201 | } 202 | 203 | 204 | } 205 | 206 | -------------------------------------------------------------------------------- /functional async.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aaaron7/functional_async_demo/4d85369ecb10b908e4fd79e29d093d644f9d4995/functional async.pdf --------------------------------------------------------------------------------