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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How await works. Support for async function #1

Closed
onmyway133 opened this issue Feb 4, 2017 · 6 comments
Closed

How await works. Support for async function #1

onmyway133 opened this issue Feb 4, 2017 · 6 comments
Assignees
Milestone

Comments

@onmyway133
Copy link

Hi, cool library 馃憤

I was reading through https://github.com/malcommac/Hydra/blob/master/Sources/Hydra/Await.swift#L109

// Create a semaphore to block the execution of the flow until
		// the promise is fulfilled or rejected
		let semaphore = DispatchSemaphore(value: 0)

so we're turning async code -> sync code by blocking the current thread?

Also, it would be cooler if there's an example project 馃槈

@malcommac
Copy link
Owner

Hi, thank you. :)
Await is a way to block the execution of current caller thread, resolve passed promise in another queue then resume the execution. In fact you are telling "await the fulfill of a promise". Promise will be resolved in another thread but the queue called by await is blocked.
This allows you to express a sync code in a sync manner.
Howewer if you don't need it you can still use Promise in classic way.
I'll also add async shortcut soon.

@malcommac
Copy link
Owner

An example of await can be found in README. I'll make a project with examples later during development; meanwhile you can take a look at unit tests in order to see how it works.

@onmyway133
Copy link
Author

@malcommac Hi, got it. Thanks 馃憤

@tomquist
Copy link

This should be mentioned in the README that await simply blocks the current thread. I first thought await magically worked similar to the C# await keyword and couldn't believe that this was even possible in Swift until I looked into the code to discover that it just blocks.

@onmyway133
Copy link
Author

@tomquist yeah, I had the same thought too

@malcommac malcommac reopened this Feb 17, 2017
@malcommac
Copy link
Owner

malcommac commented Feb 17, 2017

Yeah I think you are right; I've missed to explicitly add it to the documentation.
Here are the commits which fixes it 01ee6c5 and 09a3afa (both in 0.9.2 branch)

I've also added async operator which can works in two ways: to create a Promise or just execute a block of code.

I'm also looking a way to make it easier to write; if you have some suggestions I'll be more than happy to implement them.

@malcommac malcommac changed the title await? How await works. Support for async function Feb 17, 2017
@malcommac malcommac added this to the 0.9.2 milestone Feb 17, 2017
@malcommac malcommac self-assigned this Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants