Swift 執(zhí)行異步代碼框架 BrightFutures。
我們經(jīng)常會(huì)遇到寫(xiě)異步代碼的情況,比如在等待網(wǎng)絡(luò)響應(yīng)后更新UI,或者在要執(zhí)行耗時(shí)很長(zhǎng)的運(yùn)算后更新UI,通常我們會(huì)寫(xiě)類似下面的代碼:
User.logIn(username, password) { user, error in if !error { Posts.fetchPosts(user, success: { posts in // do something with the user's posts }, failure: handleError) } else { handleError(error) // handeError is a custom function to handle errors } }
使用BrightFutures后,代碼將是這樣的:
User.logIn(username,password).flatMap { user in Posts.fetchPosts(user) }.onSuccess { posts in // do something with the user's posts }.onFailure { error in // either logging in or fetching posts failed }