One of the most important highlights of Node.js is that it is asynchronous. At the same time its async nature can also become its Achilles' heel - if one doesn't know how to manage the nested async callback functions. more
async.waterfall(
[
function(callback) {
callback(null, 'Node.js', 'JavaScript');
},
function(arg1, arg2, callback) {
var caption = arg1 +' and '+ arg2;
callback(null, caption);
},
function(caption, callback) {
caption += ' Rock!';
callback(null, caption);
}
],
function (err, caption) {
console.log(caption);
// Node.js and JavaScript Rock!
}
);
async.series(
[
function(callback) {
callback(null, 'Node.js');
},
function(callback) {
callback(null, 'JavaScript');
},
],
function(err, response) {
// response is ['Node.js', 'JavaScript']
}
);
While the async module offers many other useful methods, only three of them are enough for most async callbacks in Node.js - series()
, parallel()
and waterfall()
.
Both these methods accept an array of functions to be executed one after another, and an optional callback function which will be executed after all the functions have executed without any errors.
The difference between series()
and waterfall()
is that the functions in series()
are executed one after another and their results stored in a response array in their corresponding indexes, and the response array is passed to the optional final callback function. In waterfall()
, the result of each function is passed on to the next function, and the result of the last function is passed to the optional final callback function.
If you need to use parallel functions executions at once and get the callback when all of them are finished, yes you guessed it use parallel()
.
// get total seconds between the times
var delta = Math.abs(date_future - date_now) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// what's left is seconds
var seconds = delta % 60; // in theory the modulus is not required
function parseSec(tt) {
var dd, hh, mm, ss;
dd = Math.floor(tt / 86400);
tt -= dd * 86400;
hh = Math.floor(tt / 3600) % 24;
tt -= hh * 3600;
mm = Math.floor(tt / 60) % 60;
tt -= mm * 60;
ss = tt % 60;
return {
dd: dd.toString(),
hh: (hh > 9) ? hh.toString() : "0" + hh,
mm: (mm > 9) ? mm.toString() : "0" + mm,
ss: (ss > 9) ? ss.toString() : "0" + ss
};
}
var up = parseSec(12532453);
console.log(up.dd + " days " + up.hh + ":" + up.mm + ":" + up.ss);
GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}
Note: You can enable GodMode using this trick in Windows Vista Window 7, Window 8, Windows 8.1,and Windows 10.
How to enable all GodMode folders?
There’s a lot more to this GodMode trick. There are a lot more GodMode folders waiting for you to utilize them. Copy and paste the script written below in a new notepad file. Rename this file as “godmodes.bat” and paste at the location where you want these GodMode folders to be. Now click on the renamed file “godmodes.bat” and there you have a list of GodMode folders, all with different uses.
That results in user@host:
for my prompt.
export PS1='\u@\h: '
If you really want something as minimalist as you ask for, try this:
export PS1='> '
You can also get creative with some colours. Here's what I use on my servers:
export PS1='\[\033[0;35m\]\h\[\033[0;33m\] \w\[\033[00m\]: '
You can attach that to the end of your ~/.bashrc
file to have it persist between logins.
\a
The 'bell' charakter\A
24h Time\d
Date (e.g. Tue Dec 21)\e
The 'escape' charakter\h
Hostname (up to the first ".")\H
Hostname\j
No. of jobs currently running (ps)\l
Current tty\n
Line feed\t
Time (hh:mm:ss)\T
Time (hh:mm:ss, 12h format)\r
Carriage return\s
Shell (i.e. bash, zsh, ksh..)\u
Username\v
Bash version\V
Full Bash release string\w
Current working directory\W
Last part of the current working directory\!
Current index in history\#
Command index\$
A "#" if you're root, else "$"\\
Literal Backslash\@
Time (12h format with am/pm)JSON.stringify
takes more optional arguments.
Try:
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, "\t"); // Indented with tab