Code Monkey home page Code Monkey logo

future's Introduction

Future

An implementation of Futures and Promises in Swift. Currently only some things are supported, but it's moving pretty quickly.

Creating a future is as simple as passing in a closure with the work you want to do asynchronously.

var testFuture: Future<String> = Future {
  return someLongRunningTask()
}

You can set onSuccess, onComplete, and onFailure callbacks for the Future. You can have as many of each of these as you want, they are not guaranteed to be run in any particular order however. Right now errors are reported simply as nil.

testFuture.onSuccess { result in
  println("Hello, \(result)")
}

testFuture.onFailure {
  println("Failed.")
}

testFuture.onComplete { result in
  if result {
    println("Hello, \(result)")
  } else {
    println("Failed.")
  }
}

In addition I'm trying to support as many combinators as I can. So far I've implemented map, which takes the result of one future and passes it into the computation of a second. It returns the second future and then you can add callbacks on the result of the combined work. You are guaranteed that the second Future won't be run until the first one has completed successfully. If the first Future fails the second Future will not be run, and the failure will be propogated forward.

var testMappedFuture = testFuture.map { result in
  return someOtherTask(result)
}

I've also added support for Promises. When you complete a Promise you also complete the future it contains. This let's you pass the result of one Future to another in-process Future.

var p = Promise<String>()
var f = p.future

var future1 = Future<String> {
	var result = someLongRunningThing()
	
	p.complete(result)
	
	return someOtherLongRunningThing()
}

var future2 = Future<String> {
	var value = doSomethingElseThatTakesTime()
	
	f.onSuccess { result in
		doSomethingWithResult(result)
	}
	
	return value
}

future's People

Contributors

danieltiger avatar

Stargazers

Stephen Belanger avatar Brad Pillow avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.