Code Monkey home page Code Monkey logo

Comments (7)

s4cha avatar s4cha commented on May 8, 2024 3

@singno Happy to announce that Async/await is available in the latest release 🎉 : https://github.com/freshOS/then/releases/tag/2.2.0

from then.

s4cha avatar s4cha commented on May 8, 2024

@singno thanks for the suggestion, I am going to check this out :)

from then.

singno avatar singno commented on May 8, 2024

@s4cha It will be very nice to have this feature!

from then.

s4cha avatar s4cha commented on May 8, 2024

@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.

s4cha avatar s4cha commented on May 8, 2024

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.

MontakOleg avatar MontakOleg commented on May 8, 2024

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.

s4cha avatar s4cha commented on May 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.