我知道node.js是一个单线程,异步,非阻塞的i/o.我已经阅读了很多相关内容.例如,PHP每个请求使用一个线程,但节点只使用一个线程,就像那样.
假设在node.js服务器上有三个请求a,b,c同时到达.其中三个请求需要大量阻塞操作,例如,他们都想读取相同的大文件.
那么请求如何排队,执行阻塞操作的顺序以及调度响应的顺序是什么?当然使用了多少线程?
请告诉我三个请求的请求到响应的顺序.
这里我从 MDN 复制了代码片段:https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
function LateBloomer() {
this.petalCount = Math.ceil(Math.random() * 12) + 1;
}
// Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
window.setTimeout(this.declare.bind(this), 1000);
};
LateBloomer.prototype.declare = function() {
console.log('I am a beautiful flower with ' +
this.petalCount + ' petals!');
};
var flower = new LateBloomer();
flower.bloom();
// after 1 second, triggers the 'declare' method
Run Code Online (Sandbox Code Playgroud)
最令人困惑的部分是: window.setTimeout(this.declare.bind(this), 1000);
我了解如何this工作并且this内部 settimeout 始终绑定到全局对象。我知道在bloom函数内部可以有var self或var。
该行中有两个 …