Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/async-plus/async-plus
⛓ A chainable interface for Swift's async/await.
https://github.com/async-plus/async-plus
async async-await await chainable concurrency flatmap future futures promise promises swift swift5
Last synced: about 2 months ago
JSON representation
⛓ A chainable interface for Swift's async/await.
- Host: GitHub
- URL: https://github.com/async-plus/async-plus
- Owner: async-plus
- License: mit
- Created: 2022-02-25T19:40:17.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T14:43:14.000Z (8 months ago)
- Last Synced: 2024-10-02T08:04:12.898Z (3 months ago)
- Topics: async, async-await, await, chainable, concurrency, flatmap, future, futures, promise, promises, swift, swift5
- Language: Swift
- Homepage: https://asyncplus.codes
- Size: 306 KB
- Stars: 190
- Watchers: 1
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-swift - async+ - A chainable interface for Swift 5.5's async/await. (Libs / Concurrency)
- awesome-swift - async+ - A chainable interface for Swift 5.5's async/await. (Libs / Concurrency)
README
Async+ for Swift provides a simple **chainable interface** for your async and throwing code, similar to promises and futures. Have the best of both worlds: you can use the async solution built into the language, but keep all the useful features of promises.
### ✏️ Usage
Basic chaining operations are:
* `.then` arranges blocks one after another, passing along any values
* `.recover` recovers from a thrown error with a backup value (or block to run)
* `.catch` catches any errors (and allows you to throw new ones for later catch blocks)
* `attempt { ... }` kicks off a chain as in the example below:```swift
attempt {
return try await getThing()
}.recover {
error in
return try await backupGetThing(error)
}.then {
thing in
await thing.doYour()
}.catch {
error in
alert(error)
}
```For comparison, if we tried to write the above flow without Async+ we'd get something like this:
```swift
Task.init {
do {
let thing: Thing
do {
thing = try await getThing()
} catch {
thing = try await backupGetThing(error)
}
await thing.doYour()
} catch {
error in
alert(error)
}
}
```Async+ allows async and/or throwing code to remain unnested, modular, and concise. For a full list of operations see the [documentation](https://docs.asyncplus.codes).
Want to still use chained code within a `do`/`catch` block, `Task.init`, or similar context? Easy: chains are fully interoperable with async and/or throwing contexts via the operations `.async()`, and `.asyncThrows()` at the end of the chain, for example:
```
let foo = await attempt{ ... }.then{ ... }.async() // non-throwing chain
let foo = try await attempt{ ... }.then{ ... }.asyncThrows() // throwing chain
```
If the chain doesn't throw you will not be able to call `asyncThrows` on it (it is a `Guarantee` type rather than a `Promise` type), and vice versa. Similarly, chains with potential for uncaught errors will raise an unused value warning at compilation time.### 💾 Installation
Async+ can be installed with either SwiftPM or CocoaPods.
For **SwiftPM**, in Xcode go to ` -> -> Package Dependencies -> "+"` and enter: `https://github.com/async-plus/async-plus.git`
Or modify your `Package.swift` file:
```swift
dependencies: [
.Package(url: "https://github.com/async-plus/async-plus.git", majorVersion: 1, minor: 1),
]
```For **CocoaPods**, in your [Podfile](https://guides.cocoapods.org/syntax/podfile.html):
```
target "Change Me!" do
pod "AsyncPlus", "~> 1.1"
end
```To use Async+ in a Swift file you must `import AsyncPlus` at the top of the file.
### 📘 Documentation
[Getting Started](https://docs.asyncplus.codes/getting-started/)
[Operations](https://docs.asyncplus.codes/operations/)
[Using chains from async or throwing contexts](https://docs.asyncplus.codes/async-or-throwing-contexts/)
[Motivation and common patterns](https://docs.asyncplus.codes/motivation-and-common-patterns/)
[Cancellation](https://docs.asyncplus.codes/cancellation/)
[Migrating from PromiseKit](https://docs.asyncplus.codes/migrating-from-promisekit/)
[Frequently asked questions (FAQ)](https://docs.asyncplus.codes/faq/)
### 🚀 Feedback and Contributing
This package is in its initial release: please provide feedback and suggestions in order to help shape the API, either by submitting an [issue](https://github.com/async-plus/async-plus/issues/new) on Github or sending a message on [Discord](https://discord.gg/vaAhGvvHpW).
Special thanks to the developers of PromiseKit and [mxcl](https://github.com/mxcl) for inspiring this work and promoting its development.