# Chain

A collection of states to chain onto.

A Chain has a start and zero or more chainable ends. If there are
zero ends, calling next() on the Chain will fail.

Extends: Chain
Implements: IChainable

- Source: [`aws-stepfunctions/lib/chain.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/aws-stepfunctions/lib/chain.ts#L14)

## Example

```ts
const map = new sfn.Map(this, 'Map State', {
  maxConcurrency: 1,
  itemsPath: sfn.JsonPath.stringAt('$.inputForMap'),
  itemSelector: {
    item: sfn.JsonPath.stringAt('$$.Map.Item.Value'),
  },
  resultPath: '$.mapOutput',
});

// The Map iterator can contain a IChainable, which can be an individual or multiple steps chained together.
// Below example is with a Choice and Pass step
const choice = new sfn.Choice(this, 'Choice');
const condition1 = sfn.Condition.stringEquals('$.item.status', 'SUCCESS');
const step1 = new sfn.Pass(this, 'Step1');
const step2 = new sfn.Pass(this, 'Step2');
const finish = new sfn.Pass(this, 'Finish');

const definition = choice
    .when(condition1, step1)
    .otherwise(step2)
    .afterwards()
    .next(finish);

map.itemProcessor(definition);
```

## Properties

- `endStates`: [INextable](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.INextable.html)[] (required) — The chainable end state(s) of this chain.

- `id`: string (required) — Identify this Chain.

- `startState`: [State](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.State.html) (required) — The start state of this chain.

## Methods

- `custom(startState: State, endStates: INextable[], lastAdded: IChainable): Chain // chainable` (static) — Make a Chain with specific start and end states, and a last-added Chainable.

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

- `sequence(start: IChainable, next: IChainable): Chain // chainable` (static) — Make a Chain with the start from one chain and the ends from another.

- `start(state: IChainable): Chain // chainable` (static) — Begin a new Chain from one chainable.

- `toSingleState(id: string, props?: ParallelProps): Parallel` — Return a single state that encompasses all states in the chain.

  This can be used to add error handling to a sequence of states.

  Be aware that this changes the result of the inner state machine
  to be an array with the result of the state machine in it. Adjust
  your paths accordingly. For example, change 'outputPath' to
  '$[0]'.
