How does Node.js (and JavaScript in general) implement the execution of asynchronous code like Promises under the hood? Does it run a new thread? Does it use Worker for that or there is some OS or libuv functionality?
var promise1 = new Promise(function(resolve, reject) {
let now = new Date();
while((now - 6000) < start){
now = new Date();
}
resolve(now - start)
});
var promise2 = new Promise(function(resolve, reject) {
let now = new Date();
while((now - 6000) < start){
now = new Date();
}
resolve(now - start)
});
let start = new Date();
promise1.then(
function(value) {
console.log("Promise 1: ", value);
}
)
promise2.then(
function(value) {
let finish = new Date();
console.log("Promise 2: ", value);
console.log(finish - start);
}
)
Promises are not asynchronous in Node.js. But there is a new feature - Worker that is asynchronous. It runs in a separate proccess. So you can use that, here is a bit more info: https://nodejs.org/api/worker_threads.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.