Node.js v4.0.0 is here!

So we lived to see it. Node.js version 4 is here which means we have the latest V8, ES6 support and the latest security patches for our favorite tool! Well the previous statement cannot describe how much good things just happened. From the creation of Node.js, through the fork of IO.js, until finally the latest version and the merger happened and now the community has the word how Node.js will be shaped from now on.

I am personally very excited about that and I am currently going to test and update one of my Docker apps that installs and uses Node.js through NVM.

I am also expecting a lot of work on this major version and the patches and new features are coming sooner than later which makes it very interesting to use Node.js these days!

Enjoy!

JavaScript: from callback hell through then-hell to generators+promises

Lately I’ve been refactoring and then refactoring and then some one of my JS apps every time I find a better way to handle the asynchronicity of the language. The app is a MEAN stack app but I am working mostly on the back-end built for Node.js with the intention to do the same for the front-end when I am satisfied with the back-end code.

So I started writing a Node.js code using the callbacks everywhere even some of my custom functions(in other apps) were made to receive and call callbacks. I was very happy how the callbacks looked like and I was writing them as the second parameter of the functions without thinking: callAsync(params, function(err, data)) all the way!

However little by little I started to add new and more complicated features and the need to nest multiple callbacks did arise. Add to that the checking for error on every callback first line and the code started to be hard to read.

At some time I discovered Promises. I decided to use the bluebird Promises package. It follows the standard and also provides additional functions that at some time are really needed. I was in love and probably still am with the then-chaining, calling async functions, attaching then() to them, returning a new async call that provides a Promise and checking the result in the next then, adding a catch() function to handle any error in the promises chain and avoiding the need to check for an error in every then(). The code was beautiful again and no pyramid of doom existed anymore.

However Promises and then-chaining created a little overhead where every then() needs a callback function and also this callback doesn’t have access to variables in the previous callback – only the result from the previous promise call. There was a need to declare helper variables in the outer scope: the function where the Promise chain is.

Then I discovered generator functions. Their basic usage doesn’t show that you can use them in place of callbacks or Promises but the fact that yield can receive a Promise makes them very interesting. Of course there is a need of a wrapper function to call the generator’s .next() and check its .done status. I decided to use the co package for that.

After all of this the code I use is similar to this one:

co(function* () {

  let user = yield User.findOneAsync({ _id: 12345678 });
  console.log('user', user);

  let user2 = yield User.findOneAsync({ _id: 112233 });
  console.log('user2', user2);

}).catch(function (err) {
  console.error(err);
});

The code above will execute synchronously. Pretty cool, huh?
User.findOneAsync() returns a Promise. yield-ing a Promise will make this type of configuration to wait for the Promise to be resolved/rejected. The resolved Promise’s value will be assigned to the variable on the left(user/user2).

Now the next thing for me is to use ES7’s async functions. I can run ES6 and ES7 code with transpilers like Babel on the back and front-end.
Async functions’ syntax is similar to the one with the generator above without the need to use a wrapper function. I am thinking to wait a little before using them and keeping the code close to what is available natively in the latest Node.js/io.js which is ES5/ES6 syntax at least until the near future(next year?) when the ES7 will be finalized and ready to be used/implemented in the JS engines.

Enable full ES6 support in Node.js – dynamic transpiling with Babel

Finally I’ve decided to go beyond Promises with Node.js and started to look how can I write easily ES6 code today in my apps. I’ve had the option to use io.js which has a good ES6 support by default, starting Node.js with the Harmony flag was also an option. However if I wanted as full as possible ES6 support I had to use Babel. There are 2 options here: static compiling and deploy or dynamic compiling when the application is ran. Babel uses cache so only the changed files are recompiled.

First I had to install babel through npm:

npm install babel --save

It is recommended to install babel globally but I wanted to have it in the local packages for easy CI testing.

Then I added this little piece of code on the top of the main app.js which is the file used when the application is started with `node app.js`:

require("babel/register");

One downside is that the main (app.js) file must contain only ES5 code if the Node.js version you are using is not supporting the new syntax. However you can just create one bootstrap.js that registers babel and includes the app.js which then can contain ES6 code(and vice versa: app.js as the main one including bootstrap.js).

I wanted to have the ES6 support in the tests too and because I use grunt, mocha and istanbul I’ve added this configuration to the Gruntfile.js:

mocha_istanbul: {
    src: 'test/server/',
    options: {
        ...
        mochaOptions: ['--compilers=js:babel/register']
    }
}

That way the tests are running with transpiled files and if I have an ES6 syntax there I am good.

I also had to add/change some configuration in .jshintrc which I use in the tests and in my IDE:

“esnext” : true, //this allows ES6 syntax
“predef”: [“-Promise”] //this allows me to use a Promise lib like bluebird instead the default ES6 implementation

I also changed the JavaScript engine in my IDE (Webstorm) from EcmaScript 5.1 to ES6 to make it happy with the new syntax.

After that I started adding some fat arrow functions and the app/tests are working. It’s interesting how the new features sometimes need more than a basic change. For example the fat arrow functions behave different with this and my code for mongoose static functions that was using return this.somefunc() needed to be changed.

If you use istanbul with the configuration above you may notice that the coverage data shows ES5 code with the source map encoded at the bottom of the pages. There’s an unstable branch on istanbul’s GitHub page about source maps which means that we’ll soon have the coverage in ES6 for free! I’ll also be waiting for the Node.js and io.js to be merged and see what works without transpilers.

How can you tell if a programmer knows Docker in 5 questions? | The Snap.hr Blog

Source: How can you tell if a programmer knows Docker in 5 questions? | The Snap.hr Blog

I struggled only on the last question about the difference between AUFS and DeviceMapper but these kind of questions always help me to find what I have to know to pass them. And I love to learn new things!

Do 404s hurt my site? | Official Google Webmaster Central Blog

I’ve been through this. Testing and redirecting wrong urls and missing pages. I have a site with a couple of million pages where soft 404 and also not found pages can increase to hundreds of thousands after a big automatic update. I managed to handle all of these pages as a single developer of my own sites by refactoring and testing the code making it more predictable so when I change multiple pages I know what to expect. Here comes the very good communication with Google’s Webmaster tools with the help of which I discover bad things on time and can even improve an already a good positioned website.

Of course one of the big things to watch out for are 404 not found and 500 server errors. So let’s read about the former here:

Source: Official Google Webmaster Central Blog: Do 404s hurt my site?

Converting Mongoose model functions to be Promise and callback friendly simultaneously

I just started using promises in one of my Node.js applications after watching the callback hell in some of the code there.

I was going to use Q but while checking the different options I stumbled upon Bluebird and I liked it.

At some time I checked how Mongoose works with promises and discovered that many functions return a promise and also the queries’ exec() returns a promise too. The Promise in Mongoose is mpromise.

However I decided to stick to a single implementation of Promise for my entire application to have something as a standard and predictable way to use “promisified” functions. And this happened to be Bluebird. It’s huge(features) and I like it!

The first thing to “clean” was a function in one of my Mongoose’s models: User.create() – a custom static function that overrides the default create function. I needed the override to be able to receive custom structured arguments and also to do some changes before the model data is saved. I changed the way I handle the create later by removing it and using pre-hooks instead but this is a good example to show and is like a repeatable template for any other function that is going to be converted the same way.

Let’s see the original Promise-free function:

userSchema.static('create', function (data, cb) {
  var user = new User({
    facebookId: data.facebookId,
    accessToken: hat(),
    name: data.name,
    photo: data.photo
  });
  return user.save(cb);
});

You can see that User.create() receives a data object literal and executes the callback cb after it’s saved.

An example usage of create will be:

User.create({
  facebookId: '68yc7j9y4f64',
  name: 'Iliyan Trifonov',
  photo:'https://...'
}, function userCreateCb (err, user) {
  if (err) {
    return console.error(err);
  }
  if (!user) {
   return console.error('User not created!');
  }
  console.info('User created successfully!', user);
});

Now let’s make User.create return a promise:

First we promisify the model with:

Promise.promisifyAll(User);
Promise.promisifyAll(User.prototype);

and then the create function will look like:

userSchema.static('create', function (data) {
  var user = new User({
    facebookId: data.facebookId,
    accessToken: hat(),
    name: data.name,
    photo: data.photo
 });
 return user.saveAsync();
});

Now we should handle the resolve and reject:

User.create({
  facebookId: '68yc7j9y4f64',
  name: 'Iliyan Trifonov',
  photo:'https://...'
}).then(function (user) {
  console.info('User created successfully!', user);
}).catch(function (err) {
  console.error('Error creating the user!', err);
});

For now I am experimenting with the then(resolveCb)/catch(errorCb) combination but it can be changed to then(resolveCb, rejectCb) or then(resolveCb)/then(null, rejectCb).

But what about the rest of the code that depends on User.create to use a callback?

Let’s add the callback argument again:

userSchema.static('create', function (data, cb) {
  var user = new User({
    facebookId: data.facebookId,
    accessToken: hat(),
    name: data.name,
    photo: data.photo
  });
  return user.saveAsync().then(function (user) {
    return cb(null, user);
  }).catch(function (err) {
    return cb(err);
  });
});

And now the create function will work with the legacy code and the new Promises code simultaneously!

 

StrongLoop | Promises in Node.js with Q – An Alternative to Callbacks

Source: StrongLoop | Promises in Node.js with Q – An Alternative to Callbacks

One thing you should use when you feel you’re reaching the pyramid of doom.

I am currently using Promises heavily in one of the projects I currently work on: Notepads. I use bluebird as it is a very nice, complete and a clever Promises library that goes beyond the original specification (and saves you time).

Performance Showdown: Node.js vs. io.js v2.0.0

We benchmarked two versions of Node.js, and two versions of io.js. Here we’ll share with you what what we found out and detail the full results!

Source: Performance Showdown: Node.js vs. io.js v2.0.0

Also check the comments there for better charts.

Update: check these graphs too.

ES6 In Depth Articles ✩ Mozilla Hacks – the Web developer blog

Articles posted in ES6 In Depth

Source: ES6 In Depth Articles ✩ Mozilla Hacks – the Web developer blog

This is an on-going collection of articles that describes ES6 like they say it: in depth. I think it’s about time for us to start learning it. It’s a good reading coming from not just anybody but Mozilla.

You can follow the future articles by using blogtrottr.com with this rss feed.