<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hugo Larrousse on Medium]]></title>
        <description><![CDATA[Stories by Hugo Larrousse on Medium]]></description>
        <link>https://medium.com/@hugolarrousse?source=rss-d36b92a30ad3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8Aj-HjILFGX-txaD.</url>
            <title>Stories by Hugo Larrousse on Medium</title>
            <link>https://medium.com/@hugolarrousse?source=rss-d36b92a30ad3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:36:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hugolarrousse/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Read/Write a file with Node.js in 2019]]></title>
            <link>https://hugolarrousse.medium.com/read-write-a-file-node-js-2019-fs-promises-d234ca00f851?source=rss-d36b92a30ad3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d234ca00f851</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sf]]></category>
            <category><![CDATA[read-file]]></category>
            <category><![CDATA[write-file]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Hugo Larrousse]]></dc:creator>
            <pubDate>Tue, 24 Sep 2019 09:36:01 GMT</pubDate>
            <atom:updated>2021-03-25T14:40:19.357Z</atom:updated>
            <content:encoded><![CDATA[<h3>Read/Write a file with Node.js 10+</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*PPvVl5iTR0Nhn1QFL7X_CA.png" /></figure><h3>Introduction</h3><p>This article shows how to write/read a file using <strong>nodejs </strong>+ <strong>async/await</strong> and <strong>without any external package</strong>. We are going to use the amazing <a href="https://nodejs.org/dist/latest-v10.x/docs/api/fs.html"><strong><em>fs</em></strong></a>,<strong><em> </em></strong>the module already included in node.js 👌</p><h3><strong>The old way: Callbacks</strong></h3><p>First of all, let’s remember how we write and read a file using the traditional and most common asynchronous way.</p><ul><li><a href="https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_readfile_path_options_callback"><em>read a file following the fs documentation:</em></a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f436f03e265b31b21a083e020dea613/href">https://medium.com/media/7f436f03e265b31b21a083e020dea613/href</a></iframe><ul><li><a href="https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback"><em>write a file following the fs documentation:</em></a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f758a650985f37cf8af1ddf5570dc6e/href">https://medium.com/media/6f758a650985f37cf8af1ddf5570dc6e/href</a></iframe><p>Reading and writing a file using callback works well but it’s not the most elegant and simple way. Moreover, if you want to write several files you will unfortunately have the “<a href="http://callbackhell.com/">callback hell</a>” problem.</p><h3><strong>The new way: Async/Await</strong></h3><p>Nowadays, async/await has become the trand when we talk about asynchronous function but before 2019 there was no way to use fs (without any other module) and async/await. Things has changed!…</p><p>Please say hello to <a href="https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_promises_api"><strong>fs Promises API</strong></a><strong> !</strong></p><p>Basically and according the fs Promises documentation:</p><blockquote>The fs.promises API provides an alternative set of asynchronous file system methods that return Promise objects rather than using callbacks</blockquote><p>An exemple? Yes, sir, right away!</p><ul><li><em>Read a file</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cb65e761ab9b470963a529b38b0918e/href">https://medium.com/media/1cb65e761ab9b470963a529b38b0918e/href</a></iframe><ul><li><em>Write a file</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62965be57758c4c0836f8da2d502e834/href">https://medium.com/media/62965be57758c4c0836f8da2d502e834/href</a></iframe><p>Voilà! You call the fs promises APi (line 1) and then you can use it just like any async function.<br>As you notice I used <strong>try/catch</strong> to grab errors.<br>That’s it, easy right?</p><p>Of course this new api provides many other methods,<a href="https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_promises_api"> I let you check the documentation</a>.</p><h3><strong>Be Careful !</strong></h3><p>3 things:</p><ul><li>This new feature is only available for <strong>Node 10 and above</strong>.</li><li>Before Node 11, you will still get a warning that this feature is experimental, but don’t worry it works perfectly fine.</li><li>Since version 11.14.0, <a href="https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V11.md#2019-04-11-version-11140-current-bethgriggs"><strong>the feature is no longer experimental</strong></a><strong> ! 😎</strong></li></ul><p>Thanks for reading! ❤</p><p>It’s my first publication on Medium so don’t hesitate to share any feedback. The next article will probably be less “technical” and about something very different like <strong>being a web developer in Brazil</strong> for exemple ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d234ca00f851" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>