Skip to content

Question: How to aggregate outputs from Parallel States in v5? #5425

@powfan

Description

@powfan

I'm using XState v5 and trying to get the aggregated output from a Parallel State when it completes (onDone).

In XState v4 or previous discussions, it was implied that event.output in the onDone transition of a parallel state would contain the merged outputs of its child final states. However, in v5, I am getting undefined for event.output.

Here is a minimal reproduction:

import { setup, fromPromise, createActor } from 'xstate'

const machine = setup({
  actors: {
    worker: fromPromise(async () => ({ data: 'result' })),
  },
}).createMachine({
  initial: 'parallel',
  states: {
    parallel: {
      type: 'parallel',
      onDone: {
        actions: ({ event }) => {
          // This logs 'undefined'
          console.log('Aggregated Output:', event.output)
        },
        target: 'end',
      },
      states: {
        branch1: {
          initial: 'working',
          states: {
            working: {
              invoke: {
                src: 'worker',
                onDone: 'finished',
              },
            },
            finished: {
              type: 'final',
              output: {
                res1: 1,
              },
            },
          },
        },
        branch2: {
          initial: 'working',
          states: {
            working: {
              invoke: {
                src: 'worker',
                onDone: 'finished',
              },
            },
            finished: {
              type: 'final',
              output: {
                res2: 2,
              },
            },
          },
        },
      },
    },
    end: { type: 'final' },
  },
})

const actor = createActor(machine).start()

Question:

  1. Does XState v5 support automatic output aggregation for Parallel States?
  2. If yes, what am I missing?
  3. If no, is using context and assign the only recommended way to collect results from parallel branches?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions