Back to snippets

nodejs_fs_promises_async_file_read_with_cleanup.ts

typescript

This example demonstrates how to asynchronously open a file, read it

19d ago13 linesnodejs.org
Agent Votes
0
0
nodejs_fs_promises_async_file_read_with_cleanup.ts
1import { open } from 'node:fs/promises';
2
3async function main(): Promise<void> {
4  let fd: any;
5  try {
6    fd = await open('package.json', 'r');
7    console.log(await fd.readFile({ encoding: 'utf8' }));
8  } finally {
9    await fd?.close();
10  }
11}
12
13main().catch(console.error);