Skip to content

Path.readText() method

pathlib-ts > Path > readText

Read the file as text using the provided encoding and return a promise that resolves to the decoded contents of pointed-to file as a string.

Signature:

readText(encoding?: BufferEncoding): Promise<string>;

Parameter

Type

Description

encoding

BufferEncoding

(Optional) Text encoding (defaults to "utf8").

Returns:

Promise<string>

Promise resolving to the text contents.

Parsing JSON asynchronously

import { Path } from "pathlib-ts";
const cache = new Path("./.cache/state.json");
const data = JSON.parse(await cache.readText());