fs.promises का उपयोग
fs.promises आपको same file functions देता है लेकिन promises return करते हुए, async/await के लिए ready।
In this page:
Syntax
const fs = require('fs/promises');
const data = await fs.readFile('path', 'utf8');
await fs.writeFile('path', data);
fs.promises
fs/promises या fs.promises से import करें। हर function एक promise return करता है, इसलिए async functions के अंदर await इस्तेमाल करें। यह parallel में कई files पढ़ने के लिए Promise.all के साथ अच्छी तरह combine होता है और servers को non-blocking रखता है।
Note:
require("fs/promises") promise API import करने का एक shorter तरीका है।
उदाहरण: fs.promises
const fs = require("fs/promises");
(async () => {
await fs.writeFile("a.txt", "AAA");
await fs.writeFile("b.txt", "BB");
const [a, b] = await Promise.all([fs.readFile("a.txt", "utf8"), fs.readFile("b.txt", "utf8")]);
console.log(a, b, (await fs.stat("a.txt")).size);
})();
// Output:
// AAA BB 3
⚠️ 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. #}
आम गलतियां
- await भूल जाना
- callbacks और promises mix करना
- parallel possible होने पर files sequentially पढ़ना
चैप्टर सारांश
- fs/promises promises return करता है
- async/await के साथ इस्तेमाल करें
- Promise.all parallel में पढ़ता है
- Non-blocking होता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: