# Fork

A fork is similar to an `on` transition in that it takes an Event and transitions the FSM to a new State.&#x20;

The difference is that a fork transitions the FSM into a concurrent region ([costate](/states/concurrent-states.md)). The `targets` of onFork are the new states the FSM will be in once the transition completes.

All the targets of a fork must enter states within the same concurrent region.

A fork is normally paired with a [join](/states/concurrent-states/join.md).  The fork causes the FSM to enter a  concurrent region, the join exits the concurrent region returning the FSM to a single state.

```
  return StateMachine.create((g) => g
    ..initialState<CheckingAir>()
    ..state<CheckingAir>((b) => b
      ..onFork<OnCheckAir>((b) => b
        ..target<HandleFan>()
        ..target<HandleLamp>(), condition: (s, e) => e.quality < 10)
      ..state<CleaningAir>((b) => b
        ..costate<HandleEquipment>((b) => b
          ..onJoin<WaitForGoodAir>((b) => b
            ..on<OnAirFlowIncreased>()
            ..on<OnLampOn>())
          ..state<HandleFan>((b) {})
          ..state<HandleLamp>((b) {}))
        ..state<WaitForGoodAir>((b) {}))));
```

All of the target states MUST be the descendant of a single [costate](/states/concurrent-states.md).

In the UML2 nomenclature a 'Fork' is a pseudo state.  Which is a state that the FSM never actually stays in but rather instantaneously transitions through.

Even thought Fork is a pseudo state, FSM2 has model it as a transition as it fits more neatly into the builder pattern that we have used.

The `onFork` method takes an Event which is the event that triggers the fork.&#x20;

```
..state<CheckingAir>((b) => b
  ..onFork<OnCheckAir>((b) => b
     ..target<HandleFan>()
     ..target<HandleLamp>()
```

In the above example, if the FSM is in the 'CheckingAir' state and an 'OnCheckAir' event occurs then the FSM enters the 'HandleFan' AND the 'HandleLamp' states which are both children of the 'HandleEquipment' concurrent region.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fsm2.onepub.dev/states/concurrent-states/fork.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
