3 ms·
Task.async already implements a result message so you're reduplicating the effort hete. You can listen for the task.async callback from LiveView.handle_info di
by throwawaymaths 3y ago
Task.async already implements a result message so you're reduplicating the effort hete. You can listen for the task.async callback from LiveView.handle_info directly, instead of having the asynchronously run function send back the message. You may want to store the ref and pid of the task in the assigns.
From the docs:
"Alternatively, if you spawn a task [via async] inside a GenServer, then the GenServer will automatically await for you and call GenServer.handle_info/2 with the task response..."
Also if you prefer sequencing the response manually the way you're doing, Task.start_link is probably more appropriate.
Note that the way you're doing it now your liveview's message queue will leak with unhandled Task.async responses and down messages (probably not a big deal given your problem domain). If you do go the Task.start_link way, you do get two way binding (if the task dies the liveview will restart itself).
- ashton314 3y agoOk, that all sounds much better. Thanks for the reply! It’s been a hot minute since I’ve used LV—so much good stuff happening. I think it’s a testament to the power of the platform that the obvious dumb solution worked so well for me. Lol
- throwawaymaths 3y agoAlways do the obvious dumb solution first!!