Async tasks with reconnection mechanism

Hi there,

I’m working on a small Elixir application which use an HTTP API called A to get xs then it try to found matches x within another of the following HTTP APIs called B-1, B-2 and B-3. For each matching x the application produce update parameters then send update request to the corresponding B-n API. Each B-n API requires its own authentication token.

Actually this how I procede:

xs = A.call()

["B-1", "B-2", "B-3"]
|> Enum.map(&B.token/1)
|> Enum.map(fn b_token ->
  Task.async(B, :find, [b_token, xs])
end)
|> Enum.map(fn task -> Task.await(task, :infinity) end)
|> Enum.map(&compare/1)
|> Enum.map(fn {b_token, params} ->
  Task.async(B, :update, [b_token, params])
end)
|> Enum.map(fn task -> Task.await(task, :infinity) end)

This actually works but it doesn’t feels right at all.

My problem is that after some time (I don’t know how much time since the B documentation doesn’t tell me anything about it) the token for the B-x API expires. To prevent the whole thing to crash I re-create a b_token within Task.async and retry the request. But by doing that every request with an authentication error will re-create a token…

So my questions are: where should I put my token so that it will update himself when it’s no more valid? And how do I make my tasks wait when the token is re-created?