Hemanth's Scribes

node

What's New in Node v10?

Author Photo

Hemanth HM

Thumbnail

Node v10 has a good set of new features, here are few that I liked.

FS Promisified

fs/promises API is an experimental promisified version of the fs functions.

const fsp = require('fs/promises');

const stat = async (dir) => fsp.stat(dir);

stat(".")
  .then(console.log, console.error)
  .catch(console.error);

## console.table
javascript
console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
// ┌─────────┬─────┬─────┐
// │ (index) │  a  │  b  │
// ├─────────┼─────┼─────┤
// │    0    │  1  │ 'Y' │
// │    1    │ 'Z' │  2  │
// └─────────┴─────┴─────┘

Top Level Await in REPL

Starting the REPL with --experimental-repl-await flag enables top level await:

$ node --experimental-repl-await
> const fsp = require('fs/promises');
> await fsp.stat(".");

## Pipeline for Streams
javascript
const fs = require('fs');
const zlib = require('zlib');
const pipeline = util.promisify(stream.pipeline);

async function run() {
  await pipeline(
    fs.createReadStream('archive.tar'),
    zlib.createGzip(),
    fs.createWriteStream('archive.tar.gz')
  );
  console.log('Pipeline succeeded');
}

run().catch(console.error);

## for-await-of Loops
javascript
async function foo() {
  const Iters = [
    Promise.resolve('foo'),
    Promise.resolve('bar'),
  ];
  
  for await (const it of Iters) {
    console.log(it);
  }
}

foo();

## Optional Catch Binding
javascript
function meow() {
  try {
    throw new Error();
  } catch { // no binding needed!
    return true;
  }
  return false;
}

## RegExp Unicode Property Escapes
javascript
(/\p{Script=Greek}/u).test('π'); // true
#node#javascript
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.