1
votes

I am building a multi of upsert and I am stuck at this part

multi =
  Enum.reduce(randoms_info, Multi.new(), fn random_info, multi ->
    Task.async(build_random_multi(multi, random_info, merchant))
  end)
  |> Enum.map(&Task.await/1)
  |> build_random_arrangement_multi(store)

After running this code the console tells me (FunctionClauseError) no function clause matching in Task.async/1 what am I doing wrong. Sorry I am new to this

2

2 Answers

0
votes

Task.async/3 spawns a process and returns a Task.t().

It’s hard to tell what are you trying to achieve, without seeing build_random_multi/3 implementation, but building Ecto.Multi (I’m assuming it’s ) is absolutely not time-consuming so I doubt it makes any sense to spawn their composing concurrently.

Task.await_many/2 would probably be the correct way to await for several tasks, but in your case, it might be directly simplified to the below without any Task but with Ecto.Multi.merge/2.

multi =
  randoms_info
  |> Enum.reduce(Multi.new(),
    &Multi.merge(&2, build_random_multi(&2, &1, merchant)))
  |> build_random_arrangement_multi(store)
0
votes

I haved solve this by using this

multi =
  Enum.reduce(randoms_info, Multi.new(), Task.async(fn ->
    build_random_multi(&2, &1, merchant)
  end))
  |> Enum.map(&Task.await/1)
  |> build_random_arrangement_multi(store)