Skip to content

Instantly share code, notes, and snippets.

@glessard
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glessard/2abbfd76d401b1b833cc to your computer and use it in GitHub Desktop.
Save glessard/2abbfd76d401b1b833cc to your computer and use it in GitHub Desktop.
Asynchronous future result in Swift. No new type.
//
// future.swift
//
// Created by Guillaume Lessard on 2014-11-14.
// Copyright (c) 2014 Guillaume Lessard. All rights reserved.
//
// https://gist.github.com/glessard/2abbfd76d401b1b833cc
//
import Dispatch
/**
A lightweight implementation for an asynchronous future result without a new type definition.
*/
/**
Execute a closure concurrently, with the ability to retrieve its result at a later time.
The returned closure will allow retrieval of the original closure's result.
If the result is ready, the call will return immediately.
If the result is not ready yet, the call will block until the result can be returned.
The result can be used multiple times; the original closure is executed only once.
This version executes the closure on a concurrently queue with a quality of service
equal to that of the calling context.
- parameter task: the closure to execute asynchronously.
- returns: a new closure with the same return type, to retrieve the result of 'task'
*/
public func future<T>(task: () -> T) -> () -> T
{
return future(dispatch_get_global_queue(qos_class_self(), 0), task: task)
}
/**
Execute a closure on a concurrent queue of a specified quality-of-service class,
with the ability to retrieve its result at a later time.
- parameter qos: the quality-of-service class to target
- parameter task: the closure to execute asynchronously.
- returns: a new closure with the same return type, to retrieve the result of 'task'
*/
public func future<T>(qos: qos_class_t, task: () -> T) -> () -> T
{
return future(dispatch_get_global_queue(qos, 0), task: task)
}
/**
Execute a closure on a specified queue, with the ability to retrieve its result at a later time.
- parameter queue: the dispatch queue to use for scheduling
- parameter task: the closure to execute asynchronously.
- returns: a new closure with the same return type, to retrieve the result of 'task'
*/
public func future<T>(queue: dispatch_queue_t, task: () -> T) -> () -> T
{
var result: T! = nil
let group = dispatch_group_create()!
dispatch_group_async(group, queue) {
result = task()
}
return {
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
return result
}
}
import Darwin
private func test()
{
let deferredResult0: () -> Double = future { return 0.0 }
let task = { () -> Int in
sleep(1)
return 10
}
let deferredResult1 = future(task)
let deferredResult2 = future { () -> Double in
sleep(2)
return 20.0
}
print("Waiting for deferred results")
print("Deferred result 0 is \(deferredResult0())")
print("Deferred result 1 is \(deferredResult1())")
print("Deferred result 2 is \(deferredResult2())")
print("Deferred result 1 is \(deferredResult1())")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment