下面這個鏈接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