# Parallel

Define a Parallel state in the state machine.

A Parallel state can be used to run one or more state machines at the same
time.

The Result of a Parallel state is an array of the results of its substatemachines.

Extends: Construct › State › Parallel
Implements: IChainable, INextable, IConstruct, IDependable

- Source: [`aws-stepfunctions/lib/states/parallel.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-stepfunctions/lib/states/parallel.ts#L81)
- Type last changed: v2.178.0 · 6 Feb 2025

## Example

```ts
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';

interface MyJobProps {
  jobFlavor: string;
}

class MyJob extends sfn.StateMachineFragment {
  public readonly startState: sfn.State;
  public readonly endStates: sfn.INextable[];

  constructor(parent: Construct, id: string, props: MyJobProps) {
    super(parent, id);

    const choice = new sfn.Choice(this, 'Choice')
      .when(sfn.Condition.stringEquals('$.branch', 'left'), new sfn.Pass(this, 'Left Branch'))
      .when(sfn.Condition.stringEquals('$.branch', 'right'), new sfn.Pass(this, 'Right Branch'));

    // ...

    this.startState = choice;
    this.endStates = choice.afterwards().endStates;
  }
}

class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    // Do 3 different variants of MyJob in parallel
    const parallel = new sfn.Parallel(this, 'All jobs')
      .branch(new MyJob(this, 'Quick', { jobFlavor: 'quick' }).prefixStates())
      .branch(new MyJob(this, 'Medium', { jobFlavor: 'medium' }).prefixStates())
      .branch(new MyJob(this, 'Slow', { jobFlavor: 'slow' }).prefixStates());

    new sfn.StateMachine(this, 'MyStateMachine', {
      definitionBody: sfn.DefinitionBody.fromChainable(parallel),
    });
  }
}
```

## Initializer

```ts
new Parallel(scope: Construct, id: string, props?: ParallelProps)
```

## Construct Props

- `arguments`: { [key: string]: any } — Parameters pass a collection of key-value pairs, either static values or JSONata expressions that select from the input.

  **Default:** No arguments

- `assign`: { [key: string]: any } — Workflow variables to store in this step.

  **Default:** Not assign variables

  Using workflow variables, you can store data in a step and retrieve that data in future steps.

- `comment`: string — A comment describing this state.

  **Default:** No comment

- `inputPath`: string = $ — JSONPath expression to select part of the state to be the input to this state.

  May also be the special value JsonPath.DISCARD, which will cause the effective
  input to be the empty object {}.

- `outputPath`: string = $ — JSONPath expression to select part of the state to be the output to this state.

  May also be the special value JsonPath.DISCARD, which will cause the effective
  output to be the empty object {}.

- `outputs`: any — Used to specify and transform output from the state.

  **Default:** $states.result or $states.errorOutput

  When specified, the value overrides the state output default.
  The output field accepts any JSON value (object, array, string, number, boolean, null).
  Any string value, including those inside objects or arrays,
  will be evaluated as JSONata if surrounded by {% %} characters.
  Output also accepts a JSONata expression directly.

- `parameters`: { [key: string]: any } — Parameters pass a collection of key-value pairs, either static values or JSONPath expressions that select from the input.

  **Default:** No parameters

- `queryLanguage`: [QueryLanguage](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.QueryLanguage.html) = JSONPath — The name of the query language used by the state.

  If the state does not contain a `queryLanguage` field,
  then it will use the query language specified in the top-level `queryLanguage` field.

- `resultPath`: string = $ — JSONPath expression to indicate where to inject the state's output.

  May also be the special value JsonPath.DISCARD, which will cause the state's
  input to become its output.

- `resultSelector`: { [key: string]: any } = None — The JSON that will replace the state's raw result and become the effective result before ResultPath is applied.

  You can use ResultSelector to create a payload with values that are static
  or selected from the state's raw result.

- `stateName`: string — Optional name for this state.

  **Default:** The construct ID will be used as state name

## Properties

- `endStates`: [INextable](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.INextable.html)[] — Continuable states of this Chainable.

- `id`: string — Descriptive identifier for this chainable.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `startState`: [State](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.State.html) — First state of this Chainable.

- `stateId`: string — Tokenized string that evaluates to the state's ID.

## Methods

- `addCatch(handler: IChainable, props?: CatchProps): Parallel // chainable` — Add a recovery handler for this state.

  When a particular error occurs, execution will continue at the error
  handler instead of failing the state machine execution.

- `addPrefix(x: string): void` — Add a prefix to the stateId of this state.

- `addRetry(props?: RetryProps): Parallel // chainable` — Add retry configuration for this state.

  This controls if and how the execution will be retried if a particular
  error occurs.

- `bindToGraph(graph: StateGraph): void` — Overwrites State.bindToGraph. Adds branches to the Parallel state here so that any necessary prefixes are appended first.

- `branch(...branches: IChainable[]): Parallel // chainable` — Define one or more branches to run in parallel.

- `filterNextables(states: State[]): INextable[]` (static) — Return only the states that allow chaining from an array of states.

- `findReachableEndStates(start: State, options?: FindStateOptions): State[]` (static) — Find the set of end states states reachable through transitions from the given start state.

- `findReachableStates(start: State, options?: FindStateOptions): State[]` (static) — Find the set of states reachable through transitions from the given start state.

  This does not retrieve states from within sub-graphs, such as states within a Parallel state's branch.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `jsonata(scope: Construct, id: string, props?: ParallelJsonataProps): Parallel // chainable` (static) — Define a Parallel state using JSONata in the state machine.

  A Parallel state can be used to run one or more state machines at the same
  time.

  The Result of a Parallel state is an array of the results of its substatemachines.

- `jsonPath(scope: Construct, id: string, props?: ParallelJsonPathProps): Parallel // chainable` (static) — Define a Parallel state using JSONPath in the state machine.

  A Parallel state can be used to run one or more state machines at the same
  time.

  The Result of a Parallel state is an array of the results of its substatemachines.

- `next(next: IChainable): Chain` — Continue normal execution with the given state.

- `prefixStates(root: IConstruct, prefix: string): void` (static) — Add a prefix to the stateId of all States found in a construct tree.

- `toStateJson(topLevelQueryLanguage?: QueryLanguage): json` — Return the Amazon States Language object for this state.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
