FutureAwaits
Lightweight library for async-await programming with Future
s or Await
written in Swift.
Follow the upcoming updates here:
Features:
- Always returns a result with either a value or a managed error.
- Possibility of running multiple functions concurrently.
- Lower level API with
Await
that returns synchrounous Result
code.
- Upper level API with
Future
, whose API is similar to Result
.
Examples
- Index:
- Using
Future
.
- Using
Result
and Await
.
Future
Future is a class that manages a Result
built with Await
/ MultiAwait
.
You can safely pass futures around and only get the result value when you want.
Here are a few usages:
Creating a Future
:
func somethingFuture() -> Future<Int, Test> {
return Future({ completion in
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
let retrievedValue = 23
if Bool.random() {
completion(.failure(Test.test))
} else {
completion(.success(retrievedValue))
}
}
})
}
Creating a Future
from Await
or any Result
:
func somethingFuture5() -> Future<[Int], Test> {
return Future(Await.default.run(
{ self.somethingAsync() },
{ self.somethingAsync() }
))
}
Getting the value of a Future
asynchronously:
somethingFuture().then { result in
print(result)
}
Getting a value or an error:
self.somethingFuture().onSuccess { value in
print(value)
}.onFailure { error in
print(error)
}
Getting the values of some Future
‘s:
(This waits one by one all the futures, for a more optimized version see Futures.combine
below)
Futures.wait(
self.somethingFuture(), self.somethingFuture2(), self.somethingFuture3()
).onSuccess { (value1, value2, value3) in
print(value1, value2, value3)
}.onFailure { error in
print(error.localizedDescription)
}
Mapping a value:
// map
self.somethingFuture()
.map { $0 * 2}
.then { result in
print(result)
}
// map + flatMap
self.somethingFuture()
.flatMap { self.somethingFuture10(value: $0) }
.map { $0 * Double(i) }
.flatMap { self.somethingFuture10(value: Int($0)) }
.then { result in
print(result)
}
Combine multiple future values:
(Concurrently runs all the futures and waits to get all the values)
Futures.combine(
self.somethingFuture(), self.somethingFuture2()
).onSuccess { results in
print(results)
}.onFailure { error in
print(error)
}
Note: you can either use combine
or combineOmittingErrors
(the later doesn’t stop on an error, and it returns a dictionary with the results)
(Lower level) Result
and Await
Creating a synchrounous Result
with the await
function (which is a helper function for the Await
struct):
func somethingAsync() -> Result<Int, Test> {
return await { completion in
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
let retrievedValue = 23
if Bool.random() {
completion(.failure(Test.test))
} else {
completion(.success(retrievedValue))
}
}
}
}
Getting the value of a function that uses Await
with the async
function:
async {
let result = somethingAsync()
// ... map the value, get the value or error, use it in other operation, etc
}
Running multiple async functions concurrently using the same Result
type.
async {
Await.default.run(
{ self.somethingAsync() },
{ self.somethingAsync() }
).onSuccess { results in // results is an array of values
print(results)
}.onFailure { error in
print(error)
}
}
Note: you can either use run
or runOmittingErrors
(the later doesn’t stop on an error, and it returns a dictionary with the results)
Note: you can either use run
or runOmittingErrors
(the later doesn’t stop on an error, and it returns a tuple with optional values)
Pro tip: use Never
as the error type if the function never returns an error
FutureAwaits
Lightweight library for async-await programming with
Future
s orAwait
written in Swift.Follow the upcoming updates here:
Features:
Await
that returns synchrounousResult
code.Future
, whose API is similar toResult
.Examples
Future
.Result
andAwait
.Future
Future is a class that manages a
Result
built withAwait
/MultiAwait
. You can safely pass futures around and only get the result value when you want.Here are a few usages:
Creating a
Future
:Creating a
Future
fromAwait
or anyResult
:Getting the value of a
Future
asynchronously:Getting a value or an error:
Getting the values of some
Future
‘s: (This waits one by one all the futures, for a more optimized version seeFutures.combine
below)Mapping a value:
Combine multiple future values: (Concurrently runs all the futures and waits to get all the values)
Note: you can either use
combine
orcombineOmittingErrors
(the later doesn’t stop on an error, and it returns a dictionary with the results)(Lower level)
Result
andAwait
Creating a synchrounous
Result
with theawait
function (which is a helper function for theAwait
struct):Getting the value of a function that uses
Await
with theasync
function:Running multiple async functions concurrently using the same
Result
type.Note: you can either use
run
orrunOmittingErrors
(the later doesn’t stop on an error, and it returns a dictionary with the results)Result
type.Note: you can either use
run
orrunOmittingErrors
(the later doesn’t stop on an error, and it returns a tuple with optional values)Pro tip: use
Never
as the error type if the function never returns an error