13 ms·
How JavaScript works: Event loop and the rise of Async programming
- Leka74 9y agoLua coroutines are a much better & simpler solution to handling async callbacks (and callback hell) than what JS offers. Here's a simple wrapper I made for it: https://gitlab.com/snippets/1678104 https://gitlab.com/snippets/1678104
- pharke 9y agoNot really seeing the advantage over modern JS async, perhaps you can explain your assertion?
- wruza 9y agoJs: async function bar(){...} function baz() {...} async function foo() { var x = await bar(); var y = baz(); return x, y; } Lua: function bar() return something_that_may_yield_deep_inside(...) end function baz() ... end function foo() local x = bar() local y = baz() return x, y end Iow, in Lua it is irrelevant whether something yields or not, so you don’t care if it is async.
- bfred_it 9y agoSo what happens to the call stack above foo? It looks like `await` is implied and the program stops until `bar` is done.
- masklinn 9y agoYeah it's a coroutine, on yield the system suspends the entire stack: http://www.lua.org/manual/5.1/manual.html#2.11 http://www.lua.org/manual/5.1/manual.html#2.11
- wruza 9y agoThe entire green thread is paused at yield() and resumed when other [controlling] thread calls resume() on it. Event loop has to account which thread yielded on which event source and resume corresponding thread when event arrives. Program as a whole doesn’t stop, only sequential blocking paths are put on waiting list. If each incoming query starts a new thread, then all queries are run concurrently around event/io loop, implicitly switching on deep yield() that programmer never spells out loud. That’s how e.g. openresty (nginx+lua) handlers work.
- bfred_it 9y agoSurely it's nice not having to worry about it, but how is that better over explicit await? If a sub-sub-sub-function suddenly decides to yield, you might not even know that your whole path is waiting for that. Right? Perhaps you need baz() to run asap but that isn't clear without looking deep into bar().
- ufo 9y agoOne big advantage is that higher order code can work with both sync and async code. The simplest example would be a for each iterator but this also applies to anything else that calls a function or method that is passed in
- vbezhenar 9y agoWell, I do want to care if it's async. I don't think it's a good design. Async keyword is making asynchronous code easier, but it's still asynchronous code with its pitfall, I think that hiding it under ordinary calls is a bad design.
- parenthephobia 9y agoBut I think I do care. When I see var x = await bar(); I know that other code, outside bar(), may have run during the execution of that statement. Also, the JS approach makes composing asynchronous operations simple: var x = bar(); var y = bar(); return await x, await y; Both bar invocations can run in parallel. If, say, each invocation of bar fires off an Ajax request that takes a few seconds to come back, that can be a significant saving. It's unclear (to me) how that would be done in Lua without complicating the API.
- wruza 9y agoOpenResty, for one, allows that via grouping queries, rather than results: local requests = { "/mysql", "/postgres", "/redis", "/memcached" } local responses = { ngx.location.capture_multi(requests) } for i, response in ipairs(responses) do print(response.foobar) end But instead ngx could lazy-evaluate responses with help of metatables (see my other comment).
- gpderetta 9y agoIt is a false security. If you call a function either you know what it does (so in lua you would know whether it call yield) or you don't and it could still be calling arbitrary code, so you have to code defensively and think about reentrancy. The only reason for await is that it normally needs to save less state (a single frame) than a full coroutine yield (a whole stack). Also lua-like stackfull coroutines don't prevent firing multiple asynchronous operations at the same time (like in your example), they only make the waiting much more peasant.
- olalonde 9y agoIn theory, someone could write a Babel plugin that wraps all files with `(async () => { /.../ })()` and prepends all function/method calls with `await ` (I think). I personally like the explicitness but maybe it's just because I'm used to it. I also wonder how that would work with `Promise.all()`.
- wruza 9y agoWe're now sorting out things for third-party js library written manually that way. It is a mess. >Promise.all() Objects returned from yields_deep_inside() may be implemented as lazy-evaluated, i.e. only `print(result.items)` or explicit `await(result)` will yield upon use, while request will be sent immediately. Thus the order of execution will depend purely on natural use case, not on programmed await sequences/groups. Since js is not parallel, you'll touch A or B first, not both. How to sort it all out is a responsibility of an event framework, not of green thread abstraction that is coroutine. One more thing: idk how js optimizes endless closures that are spawned as promise callbacks, but Lua coroutine's yield/resume is as cheap as return from / pass control to a VM. No closures are created to retain state across async calls, because VM stack is state itself. I suspect wrapping everything in async-await will simply trash GC and VM performance. In a sense, asynchrony is only emulated in javascript with a cost, though I may be wrong and it all is optimized out. We have to wait for jsvm implementation experts to [dis]prove that.
- srean 9y agoSigh. Wish we lived in the world where it was Lua that ran on the browsers and DOM was sane.
- parenthephobia 9y agoThat doesn't look simple! timer.setTimeout doesn't "simulate" a delayed callback - that's what it actually is. An example that actually uses coroutines directly rather than wrapping them up might be more convincing.
- tomxor 9y agoAsync is necessary sometimes... but I'm really hating the trend of designing APIs that make it unavoidable, with the assumption that it's necessary always. It just makes code illegible and thus buggy.
- genzoman 9y agoThe async/await paradigm of C#/Typescript fixes this very nicely IMO.
- Jizzle 9y agoI quite like async/await except that it's annoyingly easy to produce a deadlock, and given a snippet, it's not obvious that such a deadlock should occur.
- rco8786 9y agoDo you have an example? I’ve never seen a deadlock in Js. I’m trying to think of how it’s even possible? Async/await is just super around promises anyway.
- Jizzle 9y agoOops, should've specified C# specifically, in which deadlock has become notorious. I don't think there's anything wrong with async/await per se. It looks to me like the linearity of js engine event loops wouldn't produce the same problem.
- rco8786 9y agoOhh gotcha, shoulda picked up on the context :)
- zzbzq 9y agoIronically it is essentially the event loop which causes the deadlock in C#. Depending on what framework/runtime you're in .NET will schedule the await continuation on something called a "SynchronizationContext" which has ~3 different forms but it's basically an event loop/message loop which queues up each continuation on the original thread. The problem occurs when you use .Wait() or .Result instead of 'await'. This causes the function to spin waiting for the Task to finish, which of course it never will if it has a continuation trying to dispatch into that same event loop. This problem doesn't really happen at all under some circumstances, such as if the async chain starts on a background thread, or in .net core where they've removed the SynchronizationContext, hence no event loop, hence no problems.
- flavio81 9y agoThe article is a well written, well illustrated guide to programming under Javascript using an event loop, callbacks, etc. Which I personally find awkward to do in 2017 where the OS or the language runtime should give me better concurrency facilities.
- erokar 9y agoAnd there are runtimes that do that, like Erlang's BEAM. At least Node concurrency is a step in the right direction, as it's much easier to reason about than threads on the JVM.
- kuschku 9y agoJS is just introducing threads with shared state right now. Node concurrency is now as easy to reason about as JVM concurrency. Except, on the JVM, all these issues were solved years ago, and we nowadays have amazing libraries to deal with all the issues for us.
- pitaj 9y agoThere is a proposal for shared memory and atomics, which are absolutely opt-in at the moment. You can easily use the event loop if you don't need parallel processing.
- kuschku 9y agoAnd you can use an event loop in Java today if you want, the question is more about library support. And that is certainly changing in both alnguages now, in Java towards promises, in JS towards threads.
- jorgeleo 9y ago“Event loop” pfff. windows 3.1 was doing event loops before it was cool
- srean 9y agoI think the prize for doing event loops without a big fuss and before it was cool probably should go to tcl. They also did multithreading right, whereas Python botched that up. Tcl model: if you need to, run interpreters in separate threads in the same address space. No need for GIL, no need to pickle uncle pickle every message.
- glhaynes 9y ago>pickle uncle pickle Is this … a Rick and Morty thing?
- throwme211345 9y agoAmen.
- deleted 9y ago[deleted]
- Stratoscope 9y agoYou mean Windows 1.0 from 1985. Also Macintosh Toolbox from 1984. And maybe some others from that era - event loops were not a novel thing even then.
- sebcat 9y ago
- jstimpfle 9y agoSome hard earned wisdom: Callbacks (whether asynchronous or not) have the advantage of being extremely modular. The callee can just do what needs to be done - since the contract is simply a call to a function with a very simple signature, this will hardly ever be a (dependency / maintenance) burden. On the other hand, https://twitter.com/sempf/status/917962985582231552 https://twitter.com/sempf/status/917962985582231552 I suppose experienced developers do as little as possible in callbacks (basically store the event away and make a note that there was something, at a central place). Then try to do everything else in easy to understand main control flow, some time later.
- ambrop7 9y ago> I suppose experienced developers do as little as possible in callbacks (talking about C/C++ mostly) I have personally come to embrace the opposite approach: let callbacks do absolutely anything someone could do outside of a callback. This way, code gets simpler and clearer. For example, if a Socket object contained in HttpConnection calls back to HttpConnection telling it the connection is dead, the HttpConnection class should be able to just do something like "server->connections.remove(this); return;". Yes that remove is HttpConnection self-destructing; after that statement HttpConnection and the Socket no longer exist. Ensuring safety of this is really simple: whenever you call a callback, the next statement must be "return" and more specifically you must return back to who called you without touching anything which might be dead now. Or perhaps more idiomatically you can "return callback();" when both the callback and the calling function return void. In the case that you would want to do something after the callback is done, instead ask the event loop to call you "soon", and make sure your destructor will cancel that. For example this can be just a timer with zero timeout, or a special event queue designed for this purpose (in which case LIFO scheduling will get you natural "recursive" evaluation order).
- passthefist 9y ago> Or perhaps more idiomatically you can "return callback();" when both the callback and the calling function return void At least in JS I think that is idiomatic, it's an option for linting: https://eslint.org/docs/rules/callback-return https://eslint.org/docs/rules/callback-return.
- galaxyLogic 9y agoOne thing to note is that async programming like in JavaScript is "threadless" programming which means all your data-structures are by definition thread-safe. It's one less complication in your logic when you don't have to think "what if some other thread modifies this data while I am doing it?". And you never get hard-to-detect and hard-to-fix errors caused by that nor do you need to try to set up locks that might cause performance problems. So some might say that threads and locks and synchronization primitives are the solution while others might say they are the problem.
- RHSeeger 9y agoWorth noting that it is possible to have threads without the complications of concurrent modification. You just need to pass things between threads as immutable values. Tcl does it this way (prior to having threads added, it used only an event loop, like JS).
- deleted 9y ago[deleted]
- tyingq 9y agoJS webworkers add a sort of multithreading. They force an internal copy, or force a transfer of ownership of anything you postMessage() over.
- mistercow 9y agoNote though, that many of the problems that come up with threads can still come up with single-threaded async, especially when building a server. As soon as you await, any object visible to another promise chain can potentially change out from under you, and this can cause analogous problems to multithreading. One example where this can get hairy is if you have a database connection shared between two promise chains running "in parallel". You'll get undefined query ordering, which can cause very bizarre behavior. Don't get me wrong: it's still easier. But there's an interesting property I've noticed of statements of the form "by definition, problem X is impossible when you have Y", which is that X-analogous problems absolutely can happen, and they are often more complicated to solve. For another example: "by definition, memory leaks are impossible when you have a garbage collector".
- hyperion2010 9y agoChiming in a bit late here, but for years I have said that there are really only two kinds of UI that I want: a cli REPL (maybe with something like ncurses), and a full powered 3d game engine. Everything else in between is going to be a poorly implemented version of one or the other of those two, so just pick which end of the spectrum you want to be on for the purposes of rendering and user interaction, and build your browser apps like cli/ncurses, or implement them inside of quake3 and force your users to login by learning how to plasma surf.
- throwaway613834 9y agoHow in the world would a UI like that of (say) Microsoft Word be a "poorly implemented version of a CLI REPL or a 3D game engine"?
- kuschku 9y agoMS Word has a full 3D engine for text effects.
- throwaway613834 9y agoSure, and you can type text with it just like in a CLI. But how does that support the claim, is the question.
- kuschku 9y agoI’m not sure, I just hoped maybe providing some context may shed some light on it.
- hyperion2010 9y agoI guess I mean poorly implemented not in the sense that they don't work (early 90's word is an engineering marvel) but the sense that in order to get them to work you are left with enormous amounts of baggage that will come back to haunt you in the future. The 'classic' windowing toolkits and WYSIWYG implement the 2d portion of a 3d engine using a whole bunch of really cool hacks (necessary at the time) in order to run efficiently on a 286. Now days GPUs give us the luxury of not really having to worry about the cost of repainting the screen. The way QT, gtk, windows, etc. implement their interaction paradigms redraw/etc can all be subsumed by a 3d engine, and when it comes time to say, render a 3d graph inside of a hypertext document, then you don't face the huge abstraction impedance mismatch of having to drop into opengl because nothing else exits. I'm not saying that you don't need the constructs that were developed to build the classic 2 interaction paradigms, in fact you will find most if not all of them recapitulated in every 3d engine. The point is that 3d engines are essentially the logical end of any input/rendering loop you can imagine and if you develop tools long enough the infrastructure and abstractions they provide will eventually be needed and if you were using a 3d engine from the start then you won't have to implement all that functionality from the beginning, usually without the benefits of decades of experience and tens of millions of users banging on your code. To give a more concrete example -- think about how much it still sucks trying to get multimedia or good user interaction _inside_ of a word document. If word were implemented on top of a good 3d engine, then when you get the digital version of a paper the 2d projection graph is now suddenly trivial rendered because that is how it was created in the first place and the document format and editor have those capabilities from the start. Somehow this reminds me of the first programming I ever really did in Squeak [0]. 0. http://squeak.org/ http://squeak.org/
- lkrubner 9y agoI'm seeing a lot of confusion about the internals of NodeJS, so I hope to clarify: NodeJS userland is effectively single-threaded, but NodeJS is not single-threaded. NodeJS outsources some event loop scheduling to libuv, and libuv is multi-threaded (by default, 4 threads, but this can be configured. See: https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c https://medium.com/the-node-js-collection/what-you-should-kn... An excerpt: "Libuv by default creates a thread pool with four threads to offload asynchronous work to. Today’s operating systems already provide asynchronous interfaces for many I/O tasks (e.g. AIO on Linux). Whenever possible, libuv will use those asynchronous interfaces, avoiding usage of the thread pool. The same applies to third party subsystems like databases. Here the authors of the driver will rather use the asynchronous interface than utilizing a thread pool. In short: Only if there is no other way, the thread pool will be used for asynchronous I/O." It's possible there is some internal enforcement, inside of libuv, of timeouts, because there are some bizarre problems that come up when NodeJS is under enough load. See: "A surprising NodeJS failure mode: deterministic code becomes probabilistic under load" http://www.smashcompany.com/technology/a-surprising-nodejs-failure-mode-deterministic-code-becomes-probabilistic-under-load http://www.smashcompany.com/technology/a-surprising-nodejs-f...
- iaml 9y agoOn a similar note, would this kind of offloading be feasible in browsers?
- esprehn 9y agoYes, browsers already work this way today. For example in Chromium there's threads for GC, JS compilation, IO, image decoding, audio processing, video decoding, rastering, compositing, animations, indexeddb, and more. It also uses a thread pool and a task scheduler that handles prioritization both on the main thread (where the DOM is) and inside the thread pool. (ex. Handling touches is usually more important than handling xhr on the main thread). Other browsers like Servo and Firefox Quantum use the thread pool for handling layout and style tasks as well.
- edem 9y agoWhen will JS programmers pull their head out of the sand and realize that this is nothing new? All this stuff was present in Windows 1.0 and they moved on since. Now we have multi-reactors like vert.x and concurrency models based on process philosophy (Clojure). Please wake up.
- speedplane 9y agoThe hardest bugs to track down and fix are often race conditions... async has fewer than pure parallel but it doesn't eliminate them.
- kaycebasques 9y agoCan someone provide me code examples of the “job queue”?
- he0001 9y agoThe first example in this article is misleading, imo, since the actual execution is synchronous, not asynchronous. The "programming style" is asynchronous, yes, but the execution will ALWAYS be the way its described since there are no other way it could be executed and therefore synchronous. Unless there are any way of things to happen out of order in relation to the execution flow, its effectively synchronous. If there is an possibility something can be executed out of order, its asynchronous. JS doesn't provide that because the event loop. It will always be executed in the order of the loop and that will be synchronous (even if and event re-queued because it's not done yet).
- emmab 9y agoSome databases are able to provide serializability while not grabbing a global synchronous lock for all transactions. Even if the execution must appear synchronous to an observer, it's sometimes possible to do some async work. That could be because it's performing an operation which is atomic to the JS application (e.g. a sort operation can be implemented with parallelism because the JS app is paused the whole time it's running). Or, the JS interpreter could perform static analysis on control flow and determine that two or more computations do not depend on eachother for a period of execution.
- he0001 9y agoOf course the they can, but that's not the thing here and it's about JS. The "lock" here is the event loop, and that's the issue. I don't understand the part where the database is doing work is in any way related to what the JS app is doing. Even if you are running on the same machine, they are bound to have different runtime scopes and not related on how they execute.
- emmab 9y agoThe event loop doesn't strictly have to act as a global lock. It just has to appear to act as a global lock to any observer.
- 9y ago