Nodejs - Quick Tips #4

10/16/2016, Sun
Categories: #JavaScript
Tags: #NodeJs

Identify Streams Better when Logging Two or More Streams at a Time

Rxjs provides a means of performing a ‘side effect’ without modifying the observable through the observable chain. The ‘do’ command is good candidate for use when there is a need to log out information when specific operations are performed.

// Log Action of an Observable

// Provide an indication of which interval has action performed
const interval1 = Rx.Observable.interval(1000);
const example1 = interval1.do((val) =>
  console.log(`Early interval, second(s) elapsed! : ${val}`)
);

const interval = Rx.Observable.interval(2000);
const example2 = interval.do((val) =>
  console.log(`Later interval, second(s) elapsed! : ${val}\n`)
);

Observable.merge(example1, example2).subscribe((x) => {});

No ‘default’ Call Needed when ‘requiring’ transpiled module from Babel

The require call in a ES5 transpiled module from ES6 requires you to call the ‘default’ key of the return function value before the actual functionality can be accessed:

// More Boilerplate for 'Requiring' a Module

var fantasticModule = require("my-fantastic-module-name");
fantasticModule.default();

// Have to do the above instead of:
// var fantasticModule = require('my-fantastic-module-name');
// fantasticModule();