3 ms·
My favorite part was a you used &Task.await/1 :) Every time I see someone kick off a task without the await part, I want to cry a little bit.
by devoutsalsa 4y ago
My favorite part was a you used &Task.await/1 :) Every time I see someone kick off a task without the await part, I want to cry a little bit.
- PYTHONDJANGO 4y agoPlease explain, thanks.
- Jtsummers 4y agoThe pattern in the example above is a fork/join model of concurrency. Task.async will spawn a new task and return a handle to it, the task will be scheduled and run at some point. Task.await takes that handle and waits for the associated task to complete before continuing and returns its result. do_work = Task.async(fn -> long_calculation() end) That kicks off the new task, at some point later we should obtain and examine the result: result = Task.await(do_work) https://hexdocs.pm/elixir/1.13/Task.html#await/2 https://hexdocs.pm/elixir/1.13/Task.html#await/2
- PYTHONDJANGO 4y agoOK, thank you very much! But why would anybody "kick off a task without the await part"?
- jon-wood 4y agoSometimes you want something to happen, but you don’t want to block whatever you’re doing on that. Think things like sending a welcome email when someone signs up for a new account.