Giter Site home page Giter Site logo

Fix for await function about hydra HOT 2 CLOSED

malcommac avatar malcommac commented on August 15, 2024
Fix for await function

from hydra.

Comments (2)

malcommac avatar malcommac commented on August 15, 2024

It's a mistake with non labeled parameters 👎
I've solved it in b2323a9 and it will be released with 0.9.2.

Due to the fact we can't provide a behaviour like in C#, in order to avoid block of the current thread (ie. the main thread) I suggest you to do await operations in another queue by adding it inside an async closure.
That's the example fixed inside the README:

// With `async` we have just defined a Promise which will be executed in a given
// context (if omitted `background` thread is used) and return an Int value.
let asyncFunc = async({ _ -> Int // you must specify the return of the Promise, here an Int
    // With `await` the async code is resolved in a sync manner
    let loggedUser = try await(loginUser(username,pass))
    // one promise...
    let followersList = try await(getFollowers(loggedUser))
    // after another...
    let countUnfollowed = try await(unfollow(followersList))
    // ... linearly
    // Then our async promise will be resolved with the end value
    return countUnfollowed
}).then({ value in // ... and, like a promise, the value is returned
    print("Unfollowed \(value) users")
})

from hydra.

malcommac avatar malcommac commented on August 15, 2024

This is how the operator are defined:

With .. the call is allowed to throws in case of Promise's failure.

/// This define a `..` operator you can use instead of calling `await` func.
/// If you use this operator you need to also use `do/catch` in order to catch exception for rejected promises.
prefix operator ..
public prefix func ..<T> (_ promise: Promise<T>) throws -> T {
	return try awaitContext.await(promise)
}

While using ..! if it fails it will return nil.

// This define a `..!` operator you can use instead of calling `await`.
// Using this operator if promise is rejected for any reason the result is `nil` and no throws is called.
prefix operator ..!
public prefix func ..!<T> (_ promise: Promise<T>) -> T? {
	do {
		return try awaitContext.await(promise)
	} catch {
		return nil
	}
}

from hydra.

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.