Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for await function #10

Closed
ignazioc opened this issue Feb 19, 2017 · 2 comments
Closed

Fix for await function #10

ignazioc opened this issue Feb 19, 2017 · 2 comments

Comments

@ignazioc
Copy link

ignazioc commented Feb 19, 2017

I'm trying to use await using the syntax from the readme.

let city = try await(getCityPromise())

The getCityPromise function is declared like this:

func getCityPromise() -> Promise<String> { ... } 

But the compiler raises an error

cannot invoke 'await' with an argument list of type '(Promise<String>)'

Then I looked at the unit tests, and I saw that a special operator .. is used instead of calling the await function.

Using that operator my code doesn't raise any error anymore. Should the documentation be fixed or I'm missing something?

@malcommac
Copy link
Owner

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")
})

@malcommac
Copy link
Owner

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
	}
}

@malcommac malcommac changed the title Await or .. ? Fix for await function Feb 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants