Swift µframework providing Future

Related tags

EventBus Future
Overview

Future

Language CocoaPods [Carthage compatible] (https://github.com/Carthage/Carthage) License Issues

Swift µframework providing Future<T, Error>.

This library is inspired by the talk of Javier Soto at SwiftSubmit2015 and the Future implementation in Scala.

And this is using antitypical/Result.

Why we need Future?

Traditional async code
 func requestRepository(repoId: Int64, completion: (Repository?, NSError?) -> Void) {}
 func requestUser(userId: Int64, completion: (User?, NSError?) -> Void) {}
 
 // get owner info of a given repository
 requestRepository(12345) { repo, error in
 	if let repo = repo {
 		requestUser(repo.ownerId) { user, error in
 		   if let user = user {
 		       // do something
 		   } else {
 		       // error handling
 		   }
 		}
 	} else {
 		// error handling
 	}
 }
 
Code with Future
let future = requestRepository(12345)
		.map { $0.ownerId }
		.flatMap(requestUser)

future.onCompleted { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}

Shorthand by using operator

let future = requestRepository(12345) <^> { $0.ownerId } >>- requestUser

future.onCompleted { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}

Usage

  • map <^>
let f = requestUser("nghialv") <^> { $0.id }

f.onSuccess { userId in
	println(userId)
}
  • flatMap >>-
let f = searchRepositories("Hakuba") <^> { $0.first!.ownerName } >>- requestUser

f.onComplete { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}
  • filter
let e = NSError(domain: "noSuchElement", code: 1, userInfo: nil)
let f1 = searchRepositories("Hakuba")

let f = f1.filter(e){ $0.count > 0 } <^> { $0.first!.ownerName } >>- requestUser

f.onComplete { result in
	switch result {
		case .Success(let user):   println(user)
		case .Failure(let error):  println(error)
	}
}
  • andThen
// side-effect
var reposCount = 0
        
let f1 = searchRepositories("Hakuba")
let f2 = f1.andThen { result in
    switch result {
        case .Success(let repos): reposCount = repos.value.count
        case .Failure(let error): break
    }
}
let f3 = f2 <^> { $0.first!.ownerName } >>- requestUser
        
f3.onComplete { result in
    switch result {
        case .Success(let user):   println(user)
        case .Failure(let error):  println(error)
    }
}
  • recover

  • zip

let f1 = searchRepositories("Future")
let f2 = requestUser("nghialv")
        
let f3 = f1.zip(f2)

f3.onSuccess { repos, user in
	println(repos)
	println(user)
}
  • flatten

Installation

  • Using Carthage
  • Insert github "nghialv/Future" to your Cartfile
  • Run carthage update
  • Using Cocoapods
  • Insert use_frameworks! to your Podfile
  • Insert pod "Future" to your Podfile
  • Run pod install
  • Using Submodule

Requirements

  • Swift 1.2 (Xcode 6.3 or later)
  • iOS 8.0 or later
You might also like...
Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.

Promises Promises is a modern framework that provides a synchronization construct for Objective-C and Swift to facilitate writing asynchronous code. I

Easy Swift Futures & Promises.

❗️ Archived now ❗️ Since Apple released Combine framework, I decide to archive this repo. You still can use this repo as an example of Future/Promise

PromiseKit 4.5.2 with changes for Swift 5

繁體中文, 简体中文 Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master an

A promises library written in Swift featuring combinators like map, flatMap, whenAll, whenAny.
A promises library written in Swift featuring combinators like map, flatMap, whenAll, whenAny.

Promissum is a promises library written in Swift. It features some known functions from Functional Programming like, map and flatMap. It has useful co

A library that adds a throwing unwrap operator in Swift.

ThrowingUnwrap A simple package to add a throwing unwrap operator (~!) to Optionals in Swift. Import Add this to the package-wide dependencies in Pack

PJFDataSource is a small library that provides a simple, clean architecture for your app to manage its data sources while providing a consistent user interface for common content states (i.e. loading, loaded, empty, and error).
PJFDataSource is a small library that provides a simple, clean architecture for your app to manage its data sources while providing a consistent user interface for common content states (i.e. loading, loaded, empty, and error).

PJFDataSource PJFDataSource is a small library that provides a simple, clean architecture for your app to manage its data sources while providing a co

Swift µframework of simple functional programming tools

Prelude This is a Swift µframework providing a number of simple functions that I use in many of my other frameworks. Rather than continue to reimpleme

Swift µframework with extensions for the Optional Type

OptionalExtensions Why? Swift's Optional is pretty awesome, but it can always get better. This repository is an humble attempt to add some utility met

A μframework of extensions for SequenceType in Swift 2.0, inspired by Python's itertools, Haskell's standard library, and other things.

SwiftSequence Full reference here. (If you're looking for data structures in Swift, those have been moved to here) SwiftSequence is a lightweight fram

Corridor  A Coreader-like Dependency Injection μFramework
Corridor A Coreader-like Dependency Injection μFramework

Corridor A Coreader-like Dependency Injection μFramework Table of Contents Why | Examples | Usage | Installation | Credits & License | Why In order to

µframework for Attributed strings.

Attributed µframework for Attributed strings. What is Attributed? Attributed aims to be a drop in replacement to the current version of the NSAttribut

A µFramework for showing alerts like the one used when copying from pasteboard or connecting Apple pencil
A µFramework for showing alerts like the one used when copying from pasteboard or connecting Apple pencil

Drops 💧 A µFramework for showing alerts like the one used when copying from pasteboard or connecting Apple pencil. Features iOS 10+ Can be used in Sw

A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Promis The easiest Future and Promises framework in Swift. No magic. No boilerplate. Overview While starting from the Objective-C implementation of Ju

🎈 Curated collection of advanced animations that I have developed using (Swift UI for iOS) and (React Native for iOS/Android). Source code is intended to be reused by myself for future projects.
🎈 Curated collection of advanced animations that I have developed using (Swift UI for iOS) and (React Native for iOS/Android). Source code is intended to be reused by myself for future projects.

🎈 Curated collection of advanced animations that I have developed using (Swift UI for iOS) and (React Native for iOS/Android). Source code is intended to be reused by myself for future projects.

Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

Material para a apresentação da palestra "Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS" no TDC Future 2021

--- title: Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS author: Cícero Camargo date: Nov 30th

SIP calculator calculates the future value of SIP (Systematic Investment Plan) payments.
SIP calculator calculates the future value of SIP (Systematic Investment Plan) payments.

SIP calculator calculates the future value of SIP (Systematic Investment Plan) payments. This app is available in the AppStore. Learn more here. #Desc

Comments
Releases(0.0.4)
Owner
Le Van Nghia
Software Engineer. Lover of OSS. Creator of PipeCD, Promviz, LotusLoad, Hakuba, MaterialKit... 🐳
Le Van Nghia
The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Promis The easiest Future and Promises framework in Swift. No magic. No boilerplate. Overview While starting from the Objective-C implementation of Ju

Alberto De Bortoli 111 Dec 27, 2022
Material para a apresentação da palestra "Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS" no TDC Future 2021

--- title: Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS author: Cícero Camargo date: Nov 30th

Cícero Camargo 2 Nov 30, 2021
Promises for Swift & ObjC.

Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master and result in

Max Howell 14k Jan 5, 2023
Promise + progress + pause + cancel + retry for Swift.

SwiftTask Promise + progress + pause + cancel + retry for Swift. How to install See ReactKit Wiki page. Example Basic // define task let task = Task<F

ReactKit 1.9k Dec 27, 2022
When is a lightweight implementation of Promises in Swift.

Description When is a lightweight implementation of Promises in Swift. It doesn't include any helper functions for iOS and OSX and it's intentional, t

Vadym Markov 260 Oct 12, 2022
FutureLib is a pure Swift 2 library implementing Futures & Promises inspired by Scala.

FutureLib FutureLib is a pure Swift 2 library implementing Futures & Promises inspired by Scala, Promises/A+ and a cancellation concept with Cancellat

Andreas Grosam 39 Jun 3, 2021
Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Thomas Visser 1.9k Dec 20, 2022
⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Lightweight full-featured Promises, Async & Await Library in Swift What's this? Hydra is full-featured lightweight library which allows you to write b

Daniele Margutti 2k Dec 31, 2022
Promise/A+, Bluebird inspired, implementation in Swift 5

Bluebird.swift Promise/A+ compliant, Bluebird inspired, implementation in Swift 5 Features Promise/A+ Compliant Swift 5 Promise Cancellation Performan

Andrew Barba 41 Jul 11, 2022
A Promise library for Swift, based partially on Javascript's A+ spec

Promise A Promise library for Swift, based partially on Javascript's A+ spec. What is a Promise? A Promise is a way to represent a value that will exi

Soroush Khanlou 622 Nov 23, 2022