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)
Running multiple async functions concurrently using a different Result type.
async {
MultiAwait.default.run(
somethingAsync(),
somethingAsync()
).onSuccess { results in // results is a TUPLE 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 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