Skip to content

Latest commit

 

History

History
38 lines (25 loc) · 1.46 KB

9-utilities.md

File metadata and controls

38 lines (25 loc) · 1.46 KB

🐢 Node.js

🌟 The different core modules

Utilities

The util module contains various small utility functions. We use it most often to retrieve promisify which allows us to convert a callback to Promise.

import { promisify } from "util";
import stream from "stream";
import fs from "fs";

const pipeline = promisify(stream.pipeline);

await pipeline(
    fs.createReadStream("./in.txt"),
    fs.createWriteStream("./out.txt"),
);

There is also a function to switch from a Promise to a callback (callbackify). However, I never used it.

The deprecate() function can be useful if, for example, you want to deprecate a method of a package or a project. This allows developers to be notified in advance of an upcoming major SemVer update that will remove said functionality.

The inspect() method can be useful if you need to log objects in a specific way (with different levels of depth etc). This is the method used under the hood when using console methods.

import { inspect} from "util";

const log = (str) => console.log(inspect(str, { compact: false, colors: true }));
log({ data: "..." });

⬅️ 🌟 The different core modules: Child process | ➡️ 🌟 The different core modules: VM