Does V8 have an event loop?

Your intuition is right that the event loop is something that embedders should have control over. However, it is also a fundamental abstract concept of the JavaScript programming model. V8’s solution is to provide a default implementation that embedders can override; you can find it in the “libplatform” component: https://chromium.googlesource.com/v8/v8/+/master/src/libplatform/default-platform.cc#140 See also Relationship between event … Read more

What is the intention behind clause 2.2.4 of Promise/A+ spec?

The reasoning is that when the callbacks are always asynchronous instead of possibly asynchronous, it gives more consistent and reliable api to use. Consider the following code var pizza; browseStackOverflow().then(function(){ eatPizza(pizza); }); pizza = yesterdaysLeftovers; Now that snippet clearly assumes the onFulfilled will not be called right away and if that wasn’t the case we … Read more

asyncio: Is it possible to cancel a future been run by an Executor?

In this case, there is no way to cancel the Future once it has actually started running, because you’re relying on the behavior of concurrent.futures.Future, and its docs state the following: cancel() Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise … Read more

What is the difference between “event loop queue” and “job queue”?

Why “1” is after “b”? The promise specification states that all promise .then() handlers must be called asynchronously after the call stack has emptied. Thus, both a and b, which are executed synchronously on the call stack will execute before any .then() handlers so 1 will always be after a and b. Some interesting reading: … Read more

How would you implement a basic event-loop?

I used to wonder a lot about the same! A GUI main loop looks like this, in pseudo-code: void App::exec() { for(;;) { vector<Waitable> waitables; waitables.push_back(m_networkSocket); waitables.push_back(m_xConnection); waitables.push_back(m_globalTimer); Waitable* whatHappened = System::waitOnAll(waitables); switch(whatHappened) { case &m_networkSocket: readAndDispatchNetworkEvent(); break; case &m_xConnection: readAndDispatchGuiEvent(); break; case &m_globalTimer: readAndDispatchTimerEvent(); break; } } } What is a “Waitable”? Well, it’s … Read more

Do Javascript promises block the stack

When using Javascript promises, does the event loop get blocked? No. Promises are only an event notification system. They aren’t an operation themselves. They simply respond to being resolved or rejected by calling the appropriate .then() or .catch() handlers and if chained to other promises, they can delay calling those handlers until the promises they … Read more