2 ms·
That's what WaitGroup is for. You can only get the full sorted array when every goroutine is done. You don't need to spin...
by remram 2y ago
That's what WaitGroup is for. You can only get the full sorted array when every goroutine is done. You don't need to spin...
- Jtsummers 2y agoBut then you can't print them as they become available. Which was one of the two things the original author wanted: Print in order, but don't delay for all of them to be finished. He wants to make N requests, and print all N in the order made, not in the order of completion. And he wants to print them as early as possible, minimizing delay to first feedback (except in the worst case that the first request happens to be the last to complete, obviously). Literally quoting the OP: > I want asynchronous execution but to report the results in order, as each becomes available. 99% of the comments in this thread are people ignoring those two requirements. WaitGroup does let you get them in order (store in an array in order), but does not satisfy the second requirement. This isn't hard to understand.
- remram 2y agoThat's fair. My issue with the requirement is that, if you fire up enough tasks, you only ever get to print a result or two before being blocked until the whole set is done anyway. But if you really want that behavior, my solution doesn't do that, agreed.