# Continue-As-New

> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.
> For related questions, query https://docs.temporal.io/api/retrieve?q=<question>&jtb=<job-to-be-done>&src=md-page&agent=<your-name> (no auth; returns JSON `{content, source_url, page_url}`, with `source_url` pointing to Markdown for docs pages). Don't include personal data or credentials.
> If this page is wrong or missing something you needed, or Temporal behaves differently from what it says, report it with https://docs.temporal.io/api/feedback?title=<summary>&message=<what-happened>&category=documentation&page_url=/develop/typescript/workflows/continue-as-new&src=md-page&agent=<your-name> (use `category=product` when the product, not the page, is wrong). Report only a specific problem you hit, send it once, and don't include personal data or credentials.

> Use Temporal's Continue-As-New in TypeScript to manage large Event Histories by atomically creating new Workflow Executions with the same Workflow Id and fresh parameters.

This page covers the following for TypeScript developers:

- [What is Continue-As-New?](#what)
- [Use Continue-As-New](#how)
- [When is it right to Continue-As-New?](#when)
- [Test Continue-As-New](#how-to-test)

## What is Continue-As-New? 

[Continue-As-New](/workflow-execution/continue-as-new) lets a Workflow Execution close successfully and creates a new Workflow Execution.
You can think of it as a checkpoint when your Workflow gets too long or approaches certain scaling limits.

The new Workflow Execution is in the same [chain](/workflow-execution#workflow-execution-chain); it keeps the same Workflow Id but gets a new Run Id and a fresh Event History.
It also receives your Workflow's usual parameters.

## Use Continue-As-New with the TypeScript SDK 

First, design your Workflow parameters so that you can pass in the "current state" when you Continue-As-New into the next Workflow run.
This state is typically `undefined` for the original caller of the Workflow.

<!--SNIPSTART typescript-continue-as-new-workflow-->
[typescript-sandbox/src/workflows/continue-as-new/workflows.ts](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/typescript-sandbox/src/workflows/continue-as-new/workflows.ts)
```ts
export interface ClusterManagerInput {
  state?: ClusterManagerState;
  testContinueAsNew?: boolean;
}

export async function clusterManagerWorkflow(input: ClusterManagerInput = {}): Promise<ClusterManagerStateSummary> {
  const manager = new ClusterManager(input);
  wf.setHandler(startClusterSignal, () => manager.startCluster());
  wf.setHandler(shutdownClusterUpdate, () => manager.shutdownCluster());
  wf.setHandler(assignNodesToJobUpdate, (update) => manager.assignNodesToJob(update));
  wf.setHandler(getClusterStatusQuery, () => manager.getStateSummary());

  await wf.condition(() => manager.state.clusterState === ClusterState.STARTED);
  await wf.condition(
    () => manager.state.clusterState === ClusterState.SHUTTING_DOWN || manager.shouldContinueAsNew(),
  );
  if (manager.state.clusterState !== ClusterState.SHUTTING_DOWN) {
    // Wait for message handlers to finish before continuing as new
    await wf.condition(wf.allHandlersFinished);
    return await wf.continueAsNew<typeof clusterManagerWorkflow>({
      state: manager.getState(),
      testContinueAsNew: input.testContinueAsNew,
    });
  }
  return manager.getStateSummary();
}
```
<!--SNIPEND-->

The test hook in the above snippet is covered [below](#how-to-test).

Inside your Workflow, call the [`continueAsNew()`](https://typescript.temporal.io/api/namespaces/workflow#continueasnew) function with the same type.
This stops the Workflow right away and starts a new one.

<!--SNIPSTART typescript-continue-as-new-workflow {"selectedLines": ["17-24"]}-->
[typescript-sandbox/src/workflows/continue-as-new/workflows.ts](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/typescript-sandbox/src/workflows/continue-as-new/workflows.ts)
```ts
// ...
  if (manager.state.clusterState !== ClusterState.SHUTTING_DOWN) {
    // Wait for message handlers to finish before continuing as new
    await wf.condition(wf.allHandlersFinished);
    return await wf.continueAsNew<typeof clusterManagerWorkflow>({
      state: manager.getState(),
      testContinueAsNew: input.testContinueAsNew,
    });
  }
```
<!--SNIPEND-->

### Considerations for Workflows with message handlers 

If you use Updates or Signals, don't call Continue-as-New from the handlers.
Instead, wait for your handlers to finish in your main Workflow before you run `ContinueAsNew`.
See the [`allHandlersFinished`](message-passing#wait-for-message-handlers) example for guidance.

## When is it right to Continue-As-New with the TypeScript SDK? 

Use Continue-as-New when your Workflow might hit [Event History Limits](/workflow-execution/event#event-history).

Temporal tracks your Workflow's progress against these limits to let you know when you should Continue-as-New.
Call `wf.workflowInfo().continueAsNewSuggested` to check if it's time.

## Test Continue-As-New with the TypeScript SDK 

Testing Workflows that naturally Continue-as-New may be time-consuming and resource-intensive.
Instead, add a test hook to check your Workflow's Continue-as-New behavior faster in automated tests.

For example, when `testContinueAsNew == true`, this sample creates a test-only variable called `this.maxHistoryLength` and sets it to a small value.
A helper method in the Workflow checks it each time it considers using Continue-as-New:

<!--SNIPSTART typescript-continue-as-new-should-continue {"enable_code_dedenting": true}-->
[typescript-sandbox/src/workflows/continue-as-new/cluster-manager.ts](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/typescript-sandbox/src/workflows/continue-as-new/cluster-manager.ts)
```ts
shouldContinueAsNew(): boolean {
  if (wf.workflowInfo().continueAsNewSuggested) {
    return true;
  }

  // This is just for ease-of-testing. In production, we trust temporal to tell us when to continue-as-new.
  if (this.maxHistoryLength !== undefined && wf.workflowInfo().historyLength > this.maxHistoryLength) {
    return true;
  }

  return false;
}
```
<!--SNIPEND-->
