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
Futures orAwaitwritten in Swift.Follow the upcoming updates here:
Features:
Awaitthat returns synchrounousResultcode.Future, whose API is similar toResult.Examples
Future.ResultandAwait.Future
Future is a class that manages a
Resultbuilt 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
FuturefromAwaitor anyResult:Getting the value of a
Futureasynchronously: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.combinebelow)Mapping a value:
Combine multiple future values: (Concurrently runs all the futures and waits to get all the values)
Note: you can either use
combineorcombineOmittingErrors(the later doesn’t stop on an error, and it returns a dictionary with the results)(Lower level)
ResultandAwaitCreating a synchrounous
Resultwith theawaitfunction (which is a helper function for theAwaitstruct):Getting the value of a function that uses
Awaitwith theasyncfunction:Running multiple async functions concurrently using the same
Resulttype.Note: you can either use
runorrunOmittingErrors(the later doesn’t stop on an error, and it returns a dictionary with the results)Resulttype.Note: you can either use
runorrunOmittingErrors(the later doesn’t stop on an error, and it returns a tuple with optional values)Pro tip: use
Neveras the error type if the function never returns an error