elixir - ProyectWeb.PageLiveView.handle_info/2 中没有函数子句匹配
问题描述
我不知道如何匹配我的异步函数和我的 handle_info
此代码有效:
def order_month() do
Task.async(fn ->
(1..12)
|> Enum.map(fn a -> %{months: a} |> Map.put(:order_months, Proyect.BuyHandler.order_month(a |> Integer.to_string())
|> Enum.map(fn m -> m |> Proyect.BuyHandler.get_opc() end))end)
end)
end
我的意图是以这种方式接收它:
def handle_info({_ref, %{months: month, order_months: order_months}}, socket) do
{:noreply, assign(socket, %{months: month, order_months: order_months} )}
解决方案
Task.async/1
旨在产生与Task.await/2
.
无论您想用 接收结果handle_info/2
,您都应该明确地将结果从衍生(例如 with Kernel.spawn/1
)进程发送到父进程。
您没有显示 的代码Proyect.BuyHandler.get_opc/1
,但是如果我们假设它进行了简单的转换,我们可能会从那里发送消息(Task.start/1
应该在这种情况下使用而不是Task.async/1
在这种情况下启动未链接的过程。)沿着这些路线有些工作。
def order_month(pid) do
Task.start(fn ->
(1..12)
|> Enum.map(fn a ->
%{months: a,
order_months: Proyect.BuyHandler.order_month("#{a}")}
end)
|> Enum.map(&Proyect.BuyHandler.get_opc/1)
# ⇓⇓⇓⇓⇓ THIS ⇓⇓⇓⇓⇓
|> Enum.each(&send(pid, &1))
end)
end
handle_info/2
def handle_info(%{}}, socket)
在这种情况下,它本身可能应该有一个签名。
推荐阅读
- javascript - 代码拆分不适用于 webpack 4 和 razzlejs
- c# - 获取数组中我们不知道其类型的对象的索引
- ubuntu - RabbitMQ:系统尚未使用 systemd 作为 init 系统 (PID 1) 启动。无法操作
- javascript - 为什么这个for循环运行需要“”?
- kotlin - Kotlin - 尝试从 lambda 返回封闭函数时出现“此处不允许返回”错误
- docker - Rancher 1.6:禁用内置目录
- python - 返回由python类中的for循环更新的列表
- python - 如何使用 scipy optimize.minimize 函数使用共轭梯度编译 keras 序列模型?
- typescript - 当 FileReader 触发加载事件时,如何测试我的事件处理程序是否被调用
- c++ - 如何在c ++中将一个结构数组的值复制到另一个结构数组