DocsPlaygroundBlogCommunity
  • Playground
  • Blog
  • Community
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
Language Manual
Overview
  • Introduction
  • Installation
  • Editor
  • LLMs
Guides
  • Converting from JS
  • Dead Code Analysis in ReScript
  • Project Structure
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Dictionary
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
    • Execute The Lazy Computation
    • Exception Handling
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
Docs / Language Manual / Lazy Value
Edit

You are currently looking at the v12 docs, which are still a work in progress. If you miss anything, you may find it in the older v11 docs here.

Lazy Value

If you have some expensive computations you'd like to defer and cache subsequently, you can turn them into lazy values:

ReScriptJS Output
@module("node:fs")
external readdirSync: string => array<string> = "readdirSync"

// Read the directory, only once
let expensiveFilesRead = Lazy.make(() => {
  Console.log("Reading dir")
  readdirSync("./pages")
})

Note: a lazy value is not a shared data type. Don't rely on its runtime representation in your JavaScript code.

Execute The Lazy Computation

To actually run the lazy value's computation, use Lazy.get from the standard library Lazy module:

ReScriptJS Output
// First call. The computation happens
Console.log(Lazy.get(expensiveFilesRead)) // logs "Reading dir" and the directory content

// Second call. Will just return the already calculated result
Console.log(Lazy.get(expensiveFilesRead)) // logs the directory content

The first time Lazy.get is called, the expensive computation happens and the result is cached. The second time, the cached value is directly used.

You can't re-trigger the computation after the first get call. Make sure you only use a lazy value with computations whose results don't change (e.g. an expensive server request whose response is always the same).

Exception Handling

For completeness' sake, our files read example might raise an exception because of readdirSync. Here's how you'd handle it:

ReScriptJS Output
let result = try {
  Lazy.get(expensiveFilesRead)
} catch {
| Not_found => [] // empty array of files
}

Though you should probably handle the exception inside the lazy computation itself.

ExceptionPromises

© 2025 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on