Task.async and recursion

The problem is that yield_recursion is not a recursive function. After the second call to Task.yield, it returns.

This would work:

defmodule X do
  def yield_recursion(time) do
    fn ->
      Process.sleep(time)
      :success
    end
    |> Task.async()
    |> _yield_recursion()
  end

  defp _yield_recursion(async_task) do
    case Task.yield(async_task, 100) do
      nil ->
        IO.puts("recursion")
        _yield_recursion(async_task) # function calls itself == recursion

      res ->
        IO.inspect(res)
    end
  end
end

X.yield_recursion(5000)