首页 > 解决方案 > 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} )}

标签: elixirphoenix-live-view

解决方案


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/2def handle_info(%{}}, socket)在这种情况下,它本身可能应该有一个签名。


推荐阅读