下面这个链接http://greenash.net.au/thoughts/2012/11/nodejs-itself-is-blocking-only-its-io-is-non-blocking/我想两个代码无阻塞功能:的Node.js非阻塞代码示例失败
阻止代码:
function LongBucle() {
for (x=0;x<=10000;x++) {
console.log(x);
}
}
function ShortBucle() {
for (x=0;x<=10;x++) {
console.log("short "+x);
}
}
LongBucle();
console.log("Long bucle end");
ShortBucle();
console.log("Short bucle end");
现在我试图把代码变成非因此阻止代码 “console.log(”Short bucle end“);”应该先显示?
function ShortBucle(callback) {
for (x=0;x<=10;x++) {
console.log("corto "+x);
}
callback(x);
}
function LongBucle(callback) {
for (x=0;x<=10000;x++) {
console.log(x);
}
callback(x);
}
LongBucle(function(err) {
console.log('Long bucle end');
});
ShortBucle(function(err) {
console.log('short bucle end');
});
但它不起作用。我究竟做错了什么?
调用回调仍然阻塞,请使用'setImmediate' – Fluffy