Message from JavaScript discussions
April 2017
— As long as your call stack continues, it will be blocked
This is great example of the use case
Promise.resolve().then(function(){console.log("Then run");});
console.log("Running loop");
for(var i = 0; i < 100; i++) {
console.log("Blocking");
};
— When the succeding call stack finishes the then
method executes, I see
— I see it is because for that next tick the promise method switches context back to the main call stack flow. If you dont have something there in the next tick the method runs right away
— Sadly though this is not affected by closures :p
— You know how to implement a blocking sleep(ms)
?
— Yes
— (not that you would want to)
— I made this test
Promise.resolve().then(() => fs.readFileSync('./Vines.mp4')).then(() => console.log('finished'));
console.log('Lmao');
process.nextTick(() => {
console.log('next tick');
process.nextTick(() => console.log('next tick after that'))
})
And throws
Lmao
next tick
next tick after that
finished
— It's still blocking dude
— Sync calls freeze the process
— If its blocking the result doesnt have to be
Lmao
next tick
finished
next tick after that
?