Skip to content

readFile ​

readFile reads a file asynchronously and returns a Promise<Result<string, FsError>> with the file's UTF-8 contents.

SignaturereadFile(path: string): Promise<Result<string, FsError>>
tyrus
import { readFile, FsError } from "tyrus:fs";

match (await readFile("README.md")) {
    Ok(readme) => { print(readme); } // # project
    Err(error) => { printError(error.code); } // ERR_FS_READ_FAILED
}

Error Handling ​

If the file cannot be read, the fulfilled promise contains Err(FsError). The error has a stable code field (e.g. ERR_FS_READ_FAILED, ERR_FS_NOT_FOUND) and a human-readable message. Match the returned result:

error

tyrus
import { readFile, FsError } from "tyrus:fs";

match (await readFile("README.md")) {
    Ok(readme) => { print(readme); } // # project
    Err(error) => { printError(error.code); } // ERR_FS_READ_FAILED
}

With Explicit Types ​

tyrus
import { readFile, FsError } from "tyrus:fs";

const path: string = "config.json";
match (await readFile(path)) {
    Ok(contents) => { print(contents); } // { ... }
    Err(error) => { printError(error.code); } // ERR_FS_READ_FAILED
}