相关疑难解决方法(0)

不理解回调和非阻塞示例 - Node.js

在动手节点一书中,作者举了一个阻塞I\O的例子,

var post = db.query("select * from posts where id = 1");
doSomethingWithPost(post)
doSomethingElse();
Run Code Online (Sandbox Code Playgroud)

作者说在第1行完成执行db查询之前没有执行任何操作

然后,他显示了非阻塞代码

callback = function(post){
doSomethingWithPost(post)
}

db.query("select * from posts where id = 1",callback);
doSomethingElse();
Run Code Online (Sandbox Code Playgroud)

在查询执行之前,这也不会阻塞吗?

因此,在查询完成之前,不会执行doSomethingElse.

node.js

6
推荐指数
2
解决办法
5141
查看次数

标签 统计

node.js ×1