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

What is the relationship between event loop and Promise [duplicate]

Each event loop has a microtask queue and a macrotask queue. A microtask is a task that is originally to be queued on the microtask queue rather than a task queue. Refer to https://www.w3.org/TR/html51/webappapis.html#microtask-queue. There are two kinds of microtasks: solitary callback microtasks, such as Promise, and compound microtasks, such as Object.observe, MutationObserver and process.nextTick … 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

Difference between microtask and macrotask within an event loop context

One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go-around cycle. While these microtasks are processed, they can queue even … Read more