Comments (7)
@singno Happy to announce that Async/await is available in the latest release
from then.
@singno thanks for the suggestion, I am going to check this out :)
from then.
@s4cha It will be very nice to have this feature!
from then.
@singno Thanks a lot for pointing me towards this.
Never used Async/Await before and this would be a sweet addition indeed :)
Looking at it at the moment !
from then.
Example syntaxes :
// Classic
getPhotos()
.then(fetchPhotosInfo)
.then(syncPhotos)
//Async/Await
let photos = try! await(getPhotos())
try! await(fetchPhotosInfo(photos))
try! await(syncPhotos())
//Async Await + error Handling
do {
let user = try await(getPhotos()))
try await(fetchPhotosInfo(photos))
try await(syncPhotos())
} catch {
print(error)
}
While the syntax is not shorter, I don't see any drawbacks to having this syntax as well.
People are also used to the Async/Await paradigm so It'll be a very welcome feature :)
In the meantime you can try it out by adding this
typealias Async<T> = Promise<T>
typealias AsyncTask = Async<Void>
func async<T>(block:@escaping () throws -> T) -> Async<T> {
return Promise { resolve, reject in
do {
let t = try block()
resolve(t)
} catch {
reject(error)
}
}
}
func await<T>(_ promise:Promise<T>) throws -> T {
var result: T?
var error: Error?
let group = DispatchGroup()
group.enter()
promise.then { t in
result = t
group.leave()
}.onError { e in
error = e
group.leave()
}
group.wait()
if let e = error {
throw e
}
return result!
}
With which you'll be able to write :
func getPhotos() -> Async<[Photo]> {
return async {
// Do something
return [Photo(), Photo(), Photo()]
}
}
// then
let photos = try! await(getPhotos())
Have a fantastic day !
from then.
Nice approach! Unfortunately, this is not usable because current thread is blocked. Await
is 'asynchronious wait', it should immediately return control to thread and execute continuation when async work is completed. It looks like yield
in some perspective.
from then.
Yep the Dispatch queue is needed to make it concurrent :)
func async<T>(block:@escaping () throws -> T) -> Async<T> {
return Promise { resolve, reject in
DispatchQueue(label: "testqueue", attributes: .concurrent).async {
do {
let t = try block()
resolve(t)
} catch {
reject(error)
}
}
}
}
from then.
Related Issues (20)
- can oc do it? HOT 5
- Add @discardableResult to await HOT 4
- Data race HOT 4
- "Incompatible Swift version - framework was built with 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1) and the local version is 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)." HOT 5
- Progress not getting called HOT 8
- Swift 5 support HOT 6
- Library name HOT 2
- .progress should be able to return Progress, not Float HOT 1
- Resolve promise on main thread HOT 4
- Can not upload update of app to App Store after name change HOT 1
- Add watchOS platform support
- Wrapping completion handlers HOT 4
- Mapping a response to `Void` HOT 1
- Xcode 11.2.1: fatal error: Unexpected Swift version HOT 3
- Cocoapods support HOT 3
- Readme code snippet typoed HOT 1
- Thread explosion causing a deadlock? HOT 1
- Warning Xcode 12: Simulator deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set to 8.0 HOT 1
- How about support iOS 15? HOT 4
- M1 - Apple Silicon with arm64 excluded architecture issue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from then.