Await External Work
Use await when a node needs data that arrives later from outside the graph runtime.
Request one external job
Section titled “Request one external job”node :fetch_user do |state, await| user = await.call("user", :fetch_user, user_id: state[:user_id]) { user: user }endawait.call(key, kind, payload) behaves like this:
- If
resolved[key]exists, it returns the resolved value immediately. - Otherwise, if
resolve_requestreturns a value, it uses that value immediately. - Otherwise it suspends the node and emits an
AsyncGraph::Request.
Request multiple jobs at once
Section titled “Request multiple jobs at once”node :merge do |state, await| results = await.all( profile: [:fetch_profile, { user_id: state[:user_id] }], score: [:fetch_score, { user_id: state[:user_id] }] )
{ profile: results[:profile], score: results[:score] }endawait.all(...) uses any values already available through resolved: or
resolve_request: and suspends only for the remaining deferred requests. Once every
key has been resolved, it returns a symbol-keyed result hash.
Resolve known request kinds inline
Section titled “Resolve known request kinds inline”Pass resolve_request: when some request kinds can be handled immediately in-process and
only the rest should suspend:
step = graph.step( state: { left: 7, right: 5, total: 20, discount: 3 }, node: graph.entry, resolve_request: lambda do |kind, payload| case kind when :add then payload[:left] + payload[:right] when :subtract then payload[:left] - payload[:right] else AsyncGraph::DEFER end end)Return AsyncGraph::DEFER when the request still needs to be queued externally.
Resume a suspended node
Section titled “Resume a suspended node”Pass the saved node and state back into graph.step(...) together with a resolved:
hash:
resumed = graph.step( state: suspended.state, node: suspended.node, resolved: { "profile" => { name: "Ada" }, "score" => { score: 70 } })Key rules
Section titled “Key rules”- Request keys are normalized to strings internally.
await.all(...)returns symbol keys such asresults[:profile].resolve_requestreceiveskindandpayloadand must return either a value orAsyncGraph::DEFER.- Your runner should persist the mapping from request key to external job identifier.
- Pending jobs stay outside the graph runtime by design.