Ejemplo de multihilo con node

function longRunningOperation(callback) {
// simulate a 3 second operation
setTimeout(callback, 3000);
}

function userClicked() {
console.log(‘starting a long operation’);
longRunningOperation(function () {
console.log(‘ending a long operation’);
});
}
// simulate a user action
userClicked();

 

Cada uno con su closure:

function longRunningOperation(callback) {
// simulate a 3 second operation
setTimeout(callback, 3000);
}

function webRequest(request) {
console.log(‘starting a long operation for request:’, request.id);
longRunningOperation(function () {
console.log(‘ending a long operation for request:’, request.id);
});
}
// simulate a web request
webRequest({ id: 1 });
// simulate a second web request
webRequest({ id: 2 });