我知道 Node.js 通过利用事件循环具有事件驱动的异步回调的概念。
database.query("SELECT * FROM hugetable", function(rows) { var result = rows; });
console.log("Hello World");
在这里,我们不期望 database.query() 直接向我们返回结果,而是向它传递第二个参数,一个匿名函数。现在,Node.js 可以异步处理数据库请求。假设 database.query() 是异步库的一部分,Node.js 就是这样做的:就像以前一样,它接受查询并将其发送到数据库。但它不是等待它完成,而是在心里记下“当在未来某个时间数据库服务器完成并发送查询结果时,我必须执行传递给数据库.查询()。”
我正在尝试使用示例代码进行相同的操作(因为我是新手,直到 Node.js DB 交互才达到):
[root@example]# cat server8.js
function myfun(noparm , afterend)
{
for ( var i =0; i < 10; i ++)
console.log("The valus is " + i);
}
function mynextfn()
{
console.log("Hello World");
}
function afterend()
{
console.log("Hello afterend");
}
myfun(0, afterend);
mynextfn();
[root@idc-bldtool01 example]# node server8.js
The valus is 0
The valus is 1
The valus is 2
The valus is 3
The valus is 4
The valus is 5
The valus is 6
The valus is 7
The valus is 8
The valus is 9
Hello World
[root@iexample]#
因此,我没有看到“通过利用事件循环,事件驱动的异步回调的概念”?
谁能帮我实现一些基本的例子?