A couple of weeks ago, Node.js released its latest LTS: version 6.9.0. I realized this was the case because one of our services broke. It used the
nodejs:lts image and got upgraded by mistake. Inspired by this breaking of one of our services, I decided to dig into what was new in this version of Node. In this blog post, I’ll walk you through some of the new ES6 additions to Node and how they will change the code you write.
The first ES6 feature I’ll talk about is argument destructuring. With argument destructuring you can select specific properties out of an object or an array. This helps in making code a lot less verbose, especially when getting arguments for a function. For example, if I have a function that takes an object, but I don’t want to reference that object throughout my function, I can just do this:
Basically, you can assign the value of the properties in an object to a specific variable. The advantages of doing this is that you’re able to declare a schema into the properties of an object (or an array) without needing a line for every single declaration. Extracting properties into their own variables inside a function is generally a good idea because it allows your editor and linters to know more about your program. Linters like standard can tell you if a variable is not declared anywhere, but it can’t tell you if an object property is declared or not.
This syntax can also be used inside function declarations, where the object can be passed to the top of the function.
TypeError. This is something that needs to be taken into consideration when writing such code.
null by throwing the
TypeError even though
null is technically an object (typeof null). However, it treats arrays as objects, which is usually not what you would want. An interesting thing about ES6 is that an iterator can be declared for any object. This would allow the use of things like array destructuring and the use of
On top of this destructuring, there’s also the ability to add defaults to these function parameters and assignment destructuring. If any of these default variables are
undefined (and only if they’re
undefined!) the value of that property will be switched to whatever the default property value is.
Default parameters are a good way to handle the issue of destructuring assignments throwing type errors when the destructuring does not match. Still, this will only work when the value of that property is
Another interesting ES6 features is rest parameters. With rest parameters, you can take all the arguments passed to a function (for example) and join them together into a new array with all values. Rest parameters save us from having to write the verbose logic to have to turn the
arguments variable into a real array.
Rest parameters can also be used with restructuring to get only the first n arguments for a function and get all other arguments as a rest parameter. For example, if making a function that takes the name of some action and executes an operation on all the passed numbers to that function. This might not be the best way to write this function but it shows how this new syntax can be used in conjunction.
One of my favorite functions in the new version of Node is
Array.includes. This function is basically a shortcut for
arr.indexOf() !== -1, but it is a very, very nice shortcut to have. I particularly remember this function because it’s so simple, yet it never made it to Node until now.
One thing that has a lot of people in the Node.js community very happy is support for unhandled promises at a process level. On any process, you can add a
process.on('warning') to handle any promises that throw any errors not caught by the promise chain.
This process level handler can be used for logging purposes and reporting. Any error thrown by this handler can be sent to Rollbar for alerting. The only problem I’ve noticed with this is that stack traces are not very good, which might make it very hard to trace back where the unhandled error comes from.