Receive two data simultaneously and write on a map

Hi folks,
In an interview, the interviewer asked me, imagine you use a Task and send four requests to 4 APIs. When 2 or 3 requests answer at a time, and you want to write it on a map, what problem do you have in writing and how to solve it?
I could not be able to answer this question.
Thank you.

No wonder, the question sounds pretty vague. Are we talking about starting 4 separate tasks? What is the process architecture? With Erlang/Elixir, I see no problem in writing to the map since the code is serial by default.

1 Like

Yes the question is indeed vague, but I suspect the interviewer wanted you to focus on making simultaneous requests to the 4 APIs and handle the asynchronous responses. Something like:

Task.async_stream(MyApp.TaskSupervisor, ["api1", "api2", "api3", "api4"], &{&1, make_api_call(&1)})
|> Enum.map(fn {:ok, {api, api_result}} -> {api, api_result} end)
|> Enum.into(%{}) 

Result:

%{
 "api1" => result_of_api1_call,
 "api2" => result_of_api2_call,
}

That’s my best guess anyway :slight_smile:

2 Likes