The util module in Node.js provides a set of utility functions that assist with various tasks, enhancing the functionality of JavaScript and Node.js applications. It’s particularly useful for developers looking to simplify common programming tasks and improve code readability. This module is available in the Azion Runtime through Node.js compatibility, where helpers such as promisify and inspect are handy for adapting callback-based code and for logging structured data inside a function.
The example below shows how to use the util module in a function:
/** * An example of using Node.js Util API in an Azion Function. * Support: * - Partially supported (Extended by library `util`) * @module runtime-apis/nodejs/util/main * @example * // Execute with Azion Bundler: * npx edge-functions build * npx edge-functions dev */import util from "node:util";
const myTest = (callback) => { try { callback(null, "Success!"); } catch (err) { callback(err); }};
/** * An example of using the Node.js Util API in an Azion Function. * @param {*} event * @returns {Promise<Response>} */const main = async (event) => { const promisifyTest = util.promisify(myTest); const result = await promisifyTest(); console.log(util.inspect(result, { showHidden: false, depth: null }));
return new Response("Done!", { status: 200 });};
export default main;