FANDOM



utilEdit

These functions are in the module 'util'. Use require('util') to access them.

util.debug(string)Edit

A synchronous output function. Will block the process and output stringimmediately to stderr.

require('util').debug('message on stderr');

util.log(string)Edit

Output with timestamp on stdout.

require('util').log('Timestmaped message.');

util.inspect(object, showHidden=false, depth=2)Edit

Return a string representation of object, which is useful for debugging.

If showHidden is true, then the object's non-enumerable properties will be shown too.

If depth is provided, it tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects.

The default is to only recurse twice. To make it recurse indefinitely, pass innull for depth.

Example of inspecting all properties of the util object:

var util = require('util');

console.log(util.inspect(util, true, null));

util.pump(readableStream, writableStream, [callback])Edit

Experimental

Read the data from readableStream and send it to the writableStream. When writableStream.write(data) returns false readableStream will be paused until the drain event occurs on the writableStream. callbackgets an error as its only argument and is called when writableStream is closed or when an error occurs.

util.inherits(constructor, superConstructor)Edit

Inherit the prototype methods from one constructor into another. The prototype of constructor will be set to a new object created fromsuperConstructor.

As an additional convenience, superConstructor will be accessible through the constructor.super_ property.

var util = require("util");
var events = require("events");

function MyStream() {
    events.EventEmitter.call(this);
}

util.inherits(MyStream, events.EventEmitter);

MyStream.prototype.write = function(data) {
    this.emit("data", data);
}

var stream = new MyStream();

console.log(stream instanceof events.EventEmitter); // true
console.log(MyStream.super_ === events.EventEmitter); // true

stream.on("data", function(data) {
    console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.