Node.js 15! What’s changed?
Happy New month folks! ‘N’ for November, ‘N’ for Node.js, ‘N’ for New!!
This is the most important change as the Chrome V8 engine is the soul of Node.js runtime, read more about it here.
So why should you care? Because V8 defines the JavaScript language features that are available to developers. While this version bump is small, there are a few new language features that will reduce the amount of code you need to write and potentially make your code more readable.
First off, there are some new logical assignment operators to allow you to assign a value to a variable conditionally:
// new logical AND operator
let a = 1;
let b = 0;
a &&= 2;
b &&= 2;
console.log(a); // prints 2
console.log(b); // prints 0
// new logical OR operator
a = 1;
b = null;
a ||= 3;
b ||= 3;
console.log(a); // prints 1
console.log(b); // prints 3
// new logical null operator
a = 1;
b = null;
a ??= 4;
b ??= 4;
console.log(a); // prints 1
console.log(b); // prints 4
The Promise object has a new any() function that accepts one or more promises and returns the promise that resolves first. Rejected promises are ignored. If all promises are rejected, a new AggregateError is returned containing the errors from all the rejected promises.
Many developers do not have regular expression syntax committed to their brains. Luckily, the String object now has a new replaceAll() function, which means there is one less regular expression that you need to look up to replace all occurrences of a string.
myString.replaceAll(``'``regex``'``, '``function``'``)
2. Handling the 'Unhandled' rejected promises
The biggest breaking change in Node.js 15 is how unhandled rejected promises are handled. In Node.js 14, if you did not explicitly handle a rejected promise, you would see this warning:
(node:764) UnhandledPromiseRejectionWarning: something happened (Use `node --trace-warnings ...` to show where the warning was created) (node:764) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:764) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Node.js 15 now replaces this with an error:
node:internal/process/promises:218 triggerUncaughtException(err, true /* fromPromise */); ^ [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "recipe could not be generated".] { code: 'ERR_UNHANDLED_REJECTION' }
This change has the potential to impact many applications, especially for developers who ignored warning messages in the past.
Note: If you still wanna be old school or 2009 Node.js, you can run your script in command line using the new --unhandled-rejections=warn argument.
Although there many more improvements in Node.js 15 but more importantly in this post we have been able to see that Node.js runtime v8 engine has been updated from version 8.4 to 8.6; which would improve how scripts are processed by the engine. We have also seen improvement in how Unhandled rejected promises are now handled.