Skip to main content

Node.js, fs, promises

Node.js and fs

Node.js provides a general programming environment that utilizes JavaScript to perform logical operations. This allows a web development to be done entirely in JavaScript instead of needing extra languages to perform back-end processes and services. fs is a module in node.js that allows file system operation. This expands the functionality of JavaScript from mere browser language to something that can interact with the file system of the OS.

Promises, Promises

fs module traditionally used call-back function for its async operations. It has recently introduced Promise object alternatives for more robust programming. Promise object allows easy-to-read chaining of code to address the success or fail state of the given function, among many of its advantages.

Practical example - link(2)

We will be using link as an example. Link creates hard-links of the existing files. We will assume the following directory structure:
./
  - file1
  - file2
  - file3
Using the call-back async method, we can create a hard link file from file1 to file.callback as follows:
const fs = require('fs');
fs.link('file1','file.callback',
    (err)=>{
        if(err) console.log("ERROR OCCURED: ",err);
        console.log("File Link Success");
    }
);
In contrast, if I want to use the Promise method, the code would look like the following:
const fs = require('fs').promises;
fs.link('file2','file.promise')
.then( ()=>{console.log("File Link Success");} )
.catch((err)=>{console.log("ERROR OCCURED: ",err)
;
Code is more legible using the Promise object, and the error checking is done internally rather than relying on call-back.

Comments

Popular posts from this blog

Project: Boost-free goblins

The headache: Boost Don't take me wrongly: Boost library is a powerful set of tools for bleeding-edge technology. Much of it gets absorbed into the C++ Standard Libraries. The problem lies in our project being inactive for 7 years, which means it is relying on a library that is 7 years behind. Installing such an old library is a chore, and securing a pre-built package is problematic at best. The project is even incompatible with the later versions of Boost. It may have once given the original developers a powerful technology to propel their project in the past, but now we are stuck with clock stopped at 2011. Along with libtcod being stuck also in 2012, dealing with Boost is critical to bringing the project up-to-date. Options There are several ways to address the issue. Option 1 : Upgrade We can read each related source code, research how Boost has modified the relevant function calls, and either fix the expected return type or required input type, This may lead to mor...

Vehicles with a Broken Frame

Browsing through github projects in C/C++ Trying to find ways to link up the goblin_camp project with more automated testers, I started looking around the Readme.md files of other C/C++ projects in github. From neovim, I found AppVeyor, and although I have not yet set it up, it promises to supplement Travis CI with Windows test builds. I also found Coverity Scan, which might be a way for us to scan the large, preexisting code and fix goblin_camp. Then, from Cataclysm-DDA, I found Bounty Source . The icon had a dollar figures on it, so I assumed it was some sort of funnily-named fund raising site. It was, but with a twist. Show Me The Money Apparently, this site is designed to help the project by having people put down money into a pot to fix certain issues listed in Github. Some of the issues were very popular: enough people contributed to the pot of 250 dollars. I would also imagine those are tough ones. I found myself a less-popular, 4 year-old issue of 15 dollars . I am mor...

Project: Boost-free Goblins, part 3

Ready, Aim, and Fire After the battle-cry , and then calming down to make some preparation , I decided to take the shot. The target: boost::shared_ptr. To Action A quick search turns up that shared_ptr is supported in the STL's memory library since c++11. Well, that seems like an easy task. Let's take our script for a spin. tools/boostrm.sh shared_ptr memory Now I have a bunch of std::shared_ptr replacing the boost variant. Seems like everything is in good order. After pushing it into the github to have Travis CI do the honors of compiling it, turns out, some boost::weak_ptr, apparently derived from boost::shared_ptr, was being casted into boost::shared_ptr, and they are not compatible with std::shared_ptr since that's not what they inherited. Well, after a bit of research, it seems the weak_ptr has a STL version. It seems like weak_ptr needs to go, too. tools/boostrm.sh weak_ptr memory After another push into github, and another build run by Travis CI, it tu...