---
title: "How to Defer Commands"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations"
version: "6.2"
language: "nodejs"
---

# How to Defer Commands

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.md), [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/defer-operations.php.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - `Defer` allows you to register server commands via the session.
>
> - All the deferred requests will be stored in the session and sent to the server in a single batch when saveChanges is called,
>   along with any other changes/operations made on the session.  
>   Thus, all deferred commands are **executed as part of the session's saveChanges transaction**.
>
> - In this page:
>   - [Defer commands example](#defer-commands-example)
>   - [Commands that can be deferred](#commands-that-can-be-deferred)
>   - [Syntax](#syntax)

## Defer commands example

```javascript
const session = documentStore.openSession();

// Define a patchRequest object for the PatchCommandData used in the 'defer' below
const patchRequest = new PatchRequest();
patchRequest.script = "this.Supplier = 'suppliers/2-A';";

// 'defer' is available in the session's advanced methods 
session.advanced.defer(

  // Define commands to be executed:
  // i.e. Put a new document
  new PutCommandDataBase("products/999-A", null, null, {
      "Name": "My Product",
      "Supplier": "suppliers/1-A"
      "@metadata": { "@collection": "Products" }
  }),
  
  // Patch document
  new PatchCommandData("products/999-A", null, patchRequest, null),

  // Force a revision to be created
  new ForceRevisionCommandData("products/999-A"),

  // Delete a document
  new DeleteCommandData("products/1-A", null)
);

// All deferred commands will be sent to the server upon calling SaveChanges
await session.saveChanges();

}
```

## Commands that can be deferred

The following commands implement the `ICommandData` interface and can be deferred:

- PutCommandDataBase
- DeleteCommandData
- DeletePrefixedCommandData
- PatchCommandData
- BatchPatchCommandData
- PutAttachmentCommandData
- DeleteAttachmentCommandData
- CopyAttachmentCommandData
- MoveAttachmentCommandData
- CountersBatchCommandData
- PutCompareExchangeCommandData
- DeleteCompareExchangeCommandData
- CopyTimeSeriesCommandData
- TimeSeriesBatchCommandData
- ForceRevisionCommandData

## Syntax

```javascript
session.advanced.defer(...commands);
```

| Parameter | Type | Description |
| - | - | - |
| **commands** | `ICommandData[]` | Commands to be executed |

## See also

- [What is a Session and How Does it Work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md)
- [Storing Entities](https://docs.ravendb.net/6.2/client-api/session/storing-entities.nodejs.md)
- [Saving Changes](https://docs.ravendb.net/6.2/client-api/session/saving-changes.nodejs.md)
