https://toitlang.org/ logo
Waiting for a list of tasks
# help
a
Im trying sometihng like
Copy code
tasks := []
tasks.add (task (:: log.info "foo")
tasks.add (task (:: do.something "else")
tasks.do: |t| t.wait
log.info "all done"
But I believe this isn't doing what I'd expect. Is there a way to wait for a collection of actions such as tasks in this way?
k
Yes!
https://libs.toit.io/core/class-Task#group(2%2C0%2C0%2Crequired)
You can run a set of lambdas as a group individual tasks and wait for them to complete.
It is a bit tricky to write this correctly, so I recommend trying to map your problem to a call to
Task.group
.
The alternative is probably creating a
monitor.Semaphore
and down it
tasks.size
times and up it in
finally
clauses in the individual task lambdas.
Here is another example that shows the simplifications achieved when starting to use
Task.group
in Jaguar: https://github.com/toitlang/jaguar/commit/0170761a87c43ad025ddbec0e77b36ff66d9f31f.