Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

What's New in Node V10?

| Comments

node v10 has a good set of new features, the below are few of them that I liked.

FS promisified:

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

1
2
3
4
5
const fsp = require('fs/promises');
const stat = async (dir) => fsp.stat(dir);
stat(".")
.then(console.log, console.error)
.catch(console.error);

console.table:

1
2
3
4
5
6
7
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 with enable top level await, so you need not wrap it in an async function.

1
2
3
4
$ node --experimental-repl-await

> const fsp = require('fp/promises');
> await fsp.stat(".");

pipeline for streams:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);

async generators:

1
2
3
4
5
6
7
8
const answer = function *gen() {yield 42;};

var iterator = answer();
iterator.next().then(step =>
  iterator[Symbol.asyncIterator](); //iterator
  step.done; // false
  step.value; // 42
});

for-await-of-loops:

1
2
3
4
5
6
7
8
9
10
async function foo() {
    const Iters = [
        Promise.resolve('foo'),
        Promise.resolve('bar'),
    ];
    for await (const it of Iters) {
        console.log(it);
    }
}
foo();

Optional catch binding:

1
2
3
4
5
6
7
8
9
function meow(){
try {
  throw new Error();
}
catch {
  return true;
}
return false;
}

RegExp Unicode Property Escapes:

1
(/\p{Script=Greek}/u).test('π'); // true

util.types.is[…]:

No more type checks deps!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
> Object.keys(util.types)
[ 'isExternal',
  'isDate',
  'isArgumentsObject',
  'isBooleanObject',
  'isNumberObject',
  'isStringObject',
  'isSymbolObject',
  'isNativeError',
  'isRegExp',
  'isAsyncFunction',
  'isGeneratorFunction',
  'isGeneratorObject',
  'isPromise',
  'isMap',
  'isSet',
  'isMapIterator',
  'isSetIterator',
  'isWeakMap',
  'isWeakSet',
  'isArrayBuffer',
  'isDataView',
  'isSharedArrayBuffer',
  'isProxy',
  'isWebAssemblyCompiledModule',
  'isModuleNamespaceObject',
  'isAnyArrayBuffer',
  'isArrayBufferView',
  'isTypedArray',
  'isUint8Array',
  'isUint8ClampedArray',
  'isUint16Array',
  'isUint32Array',
  'isInt8Array',
  'isInt16Array',
  'isInt32Array',
  'isFloat32Array',
  'isFloat64Array',
  'isBigInt64Array',
  'isBigUint64Array' ]

Comments