---
title: "Perform requests lazily"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily"
version: "6.2"
language: "nodejs"
---

# Perform requests lazily

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/perform-operations-lazily.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**
>
> - **Lazy request**:
>
>   - You can define a lazy request within a session (e.g. a lazy-query or a lazy-load request)  
>     and defer its execution until actually needed.
>
>   - The lazy request definition is stored in the session and a `Lazy` instance is returned.  
>     The request will be sent to the server and executed only when you access the value of this instance.
>
> - **Multiple lazy requests**:
>
>   - Multiple lazy requests can be defined within the same session.
>
>   - When triggering the deferred execution (whether implicitly or explicitly),  
>     ALL pending lazy requests held up by the session will be sent to the server in a single call.  
>     This can help reduce the number of remote calls made to the server over the network.
>
> - In this page:
>   - [Requests that can be executed lazily:](#requests-that-can-be-executed-lazily)
>     - [Load entities](#loadentities)
>     - [Load entities with include](#loadwithinclude)
>     - [Load entities starting with](#loadstartingwith)
>     - [Conditional load](#conditionalload)
>     - [Run query](#runquery)
>     - [Get revisions](#getrevisions)
>     - [Get compare-exchange value](#getcompareexchange)
>   - [Multiple lazy requests](#multiple-lazy-requests)
>     - [Execute all requests - implicitly](#implicit)
>     - [Execute all requests - explicitly](#explicit)

## Operations that can be executed lazily

> **Note**
>
> &#x20;**Load entities**
>
> - ['load'](https://docs.ravendb.net/6.2/client-api/session/loading-entities.nodejs.md#load) loads a document entity from the database into the session.  
>   Loading entities can be executed **lazily**.
>
> ```javascript
> const lazyEmployee = session
>    // Add a call to lazily 
>   .advanced.lazily
>    // Document will Not be loaded from the database here, no server call is made
>   .load("employees/1-A");
>
> const employee = await lazyEmployee.getValue(); // 'load' operation is executed here
> // The employee entity is now loaded & tracked by the session
> ```

> **Note**
>
> &#x20;**Load entities with include**
>
> - ['load' with include](https://docs.ravendb.net/6.2/client-api/session/loading-entities.nodejs.md#load-with-includes) loads both the document and the specified related document.  
>   Loading entities with include can be executed **lazily**.
>
> ### Lazy\_load\_with\_include
>
> ```javascript
> const lazyProduct = session
>    // Add a call to lazily 
>   .advanced.lazily
>    // Request to include the related Supplier document
>    // Documents will Not be loaded from the database here, no server call is made
>   .include("supplierId")
>   .load("products/1-A");
>
> // 'load with include' operation will be executed here
> // Both documents will be retrieved from the database
> const product = await lazyProduct.getValue();
> // The product entity is now loaded & tracked by the session
>
> // Access the related document, no additional server call is made
> const supplier = await session.load(product.supplierId)
> // The supplier entity is now also loaded & tracked by the session
> ```
>
> ### The\_document
>
> ```javascript
> // Sample product document
> class Product {
>   constructor(name, supplierId) {
>       this.id = null;
>       this.name = name;
>       this.supplierId = supplierId; // The related document ID
>   }
> }
> ```

> **Note**
>
> &#x20;**Load entities starting with**
>
> - ['loadStartingWith'](https://docs.ravendb.net/6.2/client-api/session/loading-entities.nodejs.md#loadstartingwith) loads entities whose ID starts with the specified prefix.  
>   Loading entities with a common prefix can be executed **lazily**.
>
> ```javascript
> const lazyEmployees = session
>    // Add a call to lazily 
>   .advanced.lazily
>    // Request to load entities whose ID starts with 'employees/'
>    // Documents will Not be loaded from the database here, no server call is made
>   .loadStartingWith("employees/");
>
> const employees = await lazyEmployees.getValue(); // 'load' operation is executed here
> // The employee entities are now loaded & tracked by the session
> ```

> **Note**
>
> &#x20;**Conditional load**
>
> - ['conditionalLoad'](https://docs.ravendb.net/6.2/client-api/session/loading-entities.nodejs.md#conditionalload) logic is:
>   - If the entity is already loaded to the session:  
>     no server call is made, the tracked entity is returned.
>   - If the entity is Not already loaded to the session:  
>     the document will be loaded from the server only if the change-vector provided to the method is older than the one in the server
>     (i.e. if the document in the server is newer).
>   - Loading entities conditionally can be executed **lazily**.
>
> ```javascript
> // Create document and get its change-vector:
> {
>   const session1 = documentStore.openSession();
>
>   const employee = new Employee();
>   await session1.store(employee, "employees/1-A");
>   await session.saveChanges();
>   
>   // Get the tracked entity change-vector
>   const changeVector = session.advanced.getChangeVectorFor(employee);
> }
>
> // Conditionally lazy-load the document:
> {
>   const session2 = documentStore.openSession();
>
>   const lazyEmployee = session2
>        // Add a call to lazily 
>       .advanced.lazily
>        // Document will Not be loaded from the database here, no server call is made
>       .conditionalLoad("employees/1-A", changeVector, Employee);
>
>   const loadedItem = await lazyEmployee.getValue(); // 'conditionalLoad' operation is executed here
>   const employee = loadeditem.entity;
>   
>   // If conditionalLoad has actually fetched the document from the server (logic described above)
>   // then the employee entity is now loaded & tracked by the session
> }
> ```

> **Note**
>
> &#x20;**Run query**
>
> - A Query can be executed **lazily**.  
>   Learn more about running queries lazily in [lazy queries](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.nodejs.md).
>
> ```javascript
> // Define a lazy query:
> const lazyEmployees = session
>   .query({ collection: "employees" })
>   .whereEquals("FirstName", "John")
>    // Add a call to lazily, the query will Not be executed here 
>   .lazily();
>
> const employees = await lazyEmployees.getValue(); // Query is executed here
>
> // Note: Since query results are not projected,
> // then the resulting employee entities will be tracked by the session.
> ```

> **Note**
>
> &#x20;**Get revisions**
>
> - All methods for [getting revisions](https://docs.ravendb.net/6.2/document-extensions/revisions/client-api/session/loading.nodejs.md) and their metadata can be executed **lazily**.
>
> ```javascript
> var lazyRevisions = session
>    // Add a call to lazily 
>   .advanced.revisions.lazily
>    // Revisions will Not be fetched here, no server call is made
>   .getFor("employees/1-A");
>
>    // Usage is the same for the other get revisions methods:
>    // .get()
>    // .getMetadataFor()
>
> const revisions = lazyRevisions.getValue(); // Getting revisions is executed here
> ```

> **Note**
>
> &#x20;**Get compare-exchange value**
>
> - [Getting compare-exchange](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/compare-exchange.nodejs.md#get-compare-exchange) values can be executed **lazily**.
>
> ```javascript
> const session = documentStore.openSession({ transactionMode: "ClusterWide" });
>
> // Create compare-exchange value:
> session.advanced.clusterTransaction.createCompareExchangeValue("someKey", "someValue");
> await session.saveChanges();
>
> // Get the compare-exchange value lazily:
> const lazyCmpXchg = session
>   // Add a call to lazily 
>   .advanced.clusterTransaction.lazily
>   // Compare-exchange values will Not be fetched here, no server call is made
>   .getCompareExchangeValue("someKey");
>
> // Usage is the same for the other method:
> // .getCompareExchangeValues()
>
> const cmpXchgValue =
>   await lazyCmpXchg.getValue(); // Getting compare-exchange value is executed here            
> ```

## Multiple lazy requests

> **Note**
>
> &#x20;**Execute all requests - implicitly**
>
> Accessing the value of ANY of the lazy instances will trigger
> the execution of ALL pending lazy requests held up by the session,
> in a SINGLE server call.
>
> ```javascript
> // Define multiple lazy requests
> const lazyUser1 = session.advanced.lazily.load("users/1-A");
> const lazyUser2 = session.advanced.lazily.load("users/2-A");
>
> const lazyEmployees = session.query({ collection: "employees" })
>   .lazily();
> const lazyProducts = session.query({ collection: "products" })
>   .search("Name", "Ch*")
>   .lazily();
>
> // Accessing the value of ANY of the lazy instances will trigger
> // the execution of ALL pending lazy requests held up by the session
> // This is done in a SINGLE server call
> const user1 = await lazyUser1.getValue();
>
> // ALL the other values are now also available
> // No additional server calls are made when accessing these values
> const user2 = await lazyUser2.getValue();
> const employees = await lazyEmployees.getValue();
> const products = await lazyProducts.getValue();
> ```

> **Note**
>
> &#x20;**Execute all requests - explicitly**
>
> Explicitly calling `executeAllPendingLazyOperations` will execute
> ALL pending lazy requests held up by the session, in a SINGLE server call.
>
> ```javascript
> // Define multiple lazy requests
> const lazyUser1 = session.advanced.lazily.load("users/1-A");
> const lazyUser2 = session.advanced.lazily.load("users/2-A");
>
> const lazyEmployees = session.query({ collection: "employees" })
>   .lazily();
> const lazyProducts = session.query({ collection: "products" })
>   .search("Name", "Ch*")
>   .lazily();
>
> // Explicitly call 'executeAllPendingLazyOperations'
> // ALL pending lazy requests held up by the session will be executed in a SINGLE server call
> await session.advanced.eagerly.executeAllPendingLazyOperations();
>
> // ALL values are now available
> // No additional server calls are made when accessing the values
> const user1 = await lazyUser1.getValue();
> const user2 = await lazyUser2.getValue();
> const employees = await lazyEmployees.getValue();
> const products = await lazyProducts.getValue();
> ```

## See also

- [How to Perform Queries Lazily](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-perform-queries-lazily.nodejs.md)
- [Cluster Transaction - Overview](https://docs.ravendb.net/6.2/client-api/session/cluster-transaction/overview.nodejs.md)
