Hacker News new | past | comments | ask | show | jobs | submit login

I'm not sure if this is the most realistic example since you're implicitly relying on this being at the top level and there being a global await at the end of the block. Surely any real program will have all the work done inside a single top-level event loop.

    require 'async'

    Async do
        results = []

        Async do
          sleep 1
          results << "Hello"
        end

        puts results  # => []
    end



I just ran your example and I'm getting `puts results` line to output "Hello", just as the program intends. I'm not sure why you're getting a different result.

In any case, I'm assuring you: getting the results "out of tasks" is trivially easy.

> Surely any real program will have all the work done inside a single top-level event loop.

I don't get this. Can you please explain more what you have in mind and I'll try to help clarify things.


Sorry, I needed to add a sleep to get the behavior I wanted. Now it just prints nothing.

What I mean is I assume any real program is not going to be creating and destroying event loops any time they want to do something Async and that they'll essentially run main in a top level Async do. In fact it seems like that's the only safe thing to do with this library because the following snippet changes semantics depending on whether it's nested in an existing event loop or not.

    results = []
    Async do
      sleep 10
      results << "Hello"
    end
    puts results
So it seems like you'll pretty much always have to have an explicit wait before you can get your results in 99% of cases.


Wrap the example in a `Sync` block to get deterministic results. Now you don't have to worry if the code gets nested in an existing event loop.

    Sync do
      results = []
      Async do
        sleep 10
        results << "Hello"
      end
      puts results
    end




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: