Parallel Data Fetch
Fetch data concurrently, then combine results.
build_dashboardPending
dashboard_builder.ex
defmodule MyApp.DashboardBuilder do
use Durable
use Durable.Helpers
workflow "build_dashboard" do
step :init, fn input ->
{:ok, %{user_id: input["user_id"]}}
end
# Parallel steps produce results in __results__ map
parallel do
step :user, fn ctx ->
{:ok, %{user: Users.get(ctx.user_id)}}
end
step :orders, fn ctx ->
{:ok, %{orders: Orders.recent(ctx.user_id)}}
end
step :notifications, fn ctx ->
{:ok, %{notifs: Notifications.unread(ctx.user_id)}}
end
end
# Access results from __results__ map
step :render, fn ctx ->
results = ctx[:__results__]
# Results are tagged tuples: ["ok", data] or ["error", reason]
user = case results["user"] do
["ok", data] -> data.user
_ -> nil
end
orders = case results["orders"] do
["ok", data] -> data.orders
_ -> []
end
notifs = case results["notifications"] do
["ok", data] -> data.notifs
_ -> []
end
dashboard = Dashboard.build(user, orders, notifs)
{:ok, assign(ctx, :dashboard, dashboard)}
end
end
end
# Or use into: to transform results directly
defmodule MyApp.DashboardBuilderWithInto do
use Durable
use Durable.Helpers
workflow "build_dashboard_v2" do
step :init, fn input ->
{:ok, %{user_id: input["user_id"]}}
end
parallel into: fn ctx, results ->
# results contains tuples: %{user: {:ok, data}, orders: {:ok, data}, ...}
case {results[:user], results[:orders], results[:notifications]} do
{{:ok, user_data}, {:ok, orders_data}, {:ok, notifs_data}} ->
{:ok, Map.merge(ctx, %{
user: user_data.user,
orders: orders_data.orders,
notifs: notifs_data.notifs
})}
_ ->
{:error, "Failed to fetch dashboard data"}
end
end do
step :user, fn ctx -> {:ok, %{user: Users.get(ctx.user_id)}} end
step :orders, fn ctx -> {:ok, %{orders: Orders.recent(ctx.user_id)}} end
step :notifications, fn ctx -> {:ok, %{notifs: Notifications.unread(ctx.user_id)}} end
end
step :render, fn ctx ->
dashboard = Dashboard.build(ctx.user, ctx.orders, ctx.notifs)
{:ok, assign(ctx, :dashboard, dashboard)}
end
end
endThe code is the README example. The run is simulated in your browser, with time sped up. For the details, read the parallel guide.