← Back to Node.js Course | Chapter 2: Core Modules | Lesson 5 of 7

util मॉड्यूल

util module promisify, format, और inspect जैसे handy helpers collect करता है।

In this page:

  1. util Module
Syntax
javascript
const util = require('util');
const promisified = util.promisify(callbackFunction);
const result = await promisified(arg);

util Module

util.promisify एक callback-style function को एक ऐसे function में बदलता है जो एक promise return करता है। util.format placeholders से strings बनाता है और util.inspect depth पर control के साथ objects print करता है। util.types और util.isDeepStrictEqual checks में मदद करते हैं।

Note: fs.promises पहले से exist करता है, इसलिए promisify पुराने callback APIs के लिए सबसे useful है।

उदाहरण: util module

javascript
const util = require("util");
const wait = util.promisify((ms, cb) => setTimeout(() => cb(null, "done after " + ms), ms));
wait(10).then(console.log);
console.log(util.format("%s scored %d", "Ada", 95));
console.log(util.inspect({ a: { b: { c: 1 } } }, { depth: 0 }));

// Output:
// Ada scored 95
// { a: [Object] }
// done after 10

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ऐसे functions promisify करना जो error-first callback convention follow नहीं करते
  2. deprecated util.isArray इस्तेमाल करना
  3. deep objects log करना और [Object] देखना
चैप्टर सारांश
  • promisify callbacks को promises में convert करता है
  • format %s और %d placeholders इस्तेमाल करता है
  • inspect object printing control करता है
  • Callbacks error-first होने चाहिए
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.