node.js event-driven

Copy from Understanding node.js

Node is basically very good when you need to do several things at the same time. Have you ever written a piece of code and said "I wish this would run in parallel"? Well, in node everything runs in parallel, except your code.

When you need to do several things at once, Node basically very good. Have you ever written a piece of code and say "I hope it will run in parallel"? In addition to the code in the node, all things are parallel.

"Huh?"
That's right, everything runs in parallel, except your code. To understand that, imagine your code is the king, and node is his army of servants.

Imagine your code is Big Brother, node is his messenger.

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among his colleagues and they get to work.

That day cowboys wake brother and asked him what to do. Big Brother is given a task list and then continue to sleep liao, assign tasks to other cowboys cowboys together to do it.

Once a servant finishes a task, he lines up outside the kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king will give the servant more tasks on the way out.
Life is good, for the king's servants carry out all of his tasks in parallel, but only report with one result at a time, so the king can focus. *

Whenever a messenger harvested protection money, he would line up outside the Big Brother house Big Brother report to each brother just listen to a report on the work of cowboys, sometimes complete report will be out in front of the cowboys cowboys in arrangements to other tasks (ie callback, sister to buy bags and the like)

"That's fantastic, but could you quit the silly metaphor and speak geek to me?"
Sure. A simple node program may look like this:

Use the following code snippet explain

var fs = require('fs')
  , sys = require('sys');

fs.readFile('treasure-chamber-report.txt', function(report) {
  sys.puts("oh, look at all my money: "+report);
});

fs.writeFile('letter-to-princess.txt', '...', function() {
  sys.puts("can't wait to hear back from her!");
});

Your code gives node the two tasks to read and write a file, and then goes to sleep. Once node has completed a task, the callback for it is fired. But there can only be one callback firing at the same time. Until that callback has finished executing, all other callbacks have to wait in line. In addition to that, there is no guarantee on the order in which the callbacks will fire.

This code allows node to read and write two tasks, then node will continue to sleep. Until a task is completed, he triggered the callback, but at the same time only one callback function is executed (the rest of the callback function will be arranged in a queue one by one execution order) . Because tasks are executed in parallel, we can not guarantee the order triggered the callback function (which is why the so-called callback hell).

This is single-threaded JavaScripts / event loop design beauty ~

Guess you like

Origin www.cnblogs.com/riwang/p/riwang.html