News for hackers. Stuff that matters.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by tlarkworthy on Thursday August 06, 2020 @12:18AM (#24068090)
Wow that was informative. So ES Modules is really about the Red functions [1] getting module support.

I find it fascinating watching our understanding of async computation mature over the years.

[1] http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by samth on Thursday August 06, 2020 @07:24AM (#24069974)
No, this is not correct. The split long predates async in JS The fundamental reason behind the difference between ES modules and CJS is that require() does synchronous I/O in a function, but that's not acceptable in the browser context. So you either need to make require an asynchronous function (meaning with a callback, back in 2010 when this happened) or move the I/O out of the execution of module itself, as import does.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by z3t4 on Thursday August 06, 2020 @03:04PM (#24074309)
Sync xhr is being depricated. Meanwhile Google and Facebook analytics/tracking freeze the main thread for a whooping two seconds. Why not push the module dependencies with http2 push? The problem is not technical. Its big money wanting to break backwards compatibility.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by WorldMaker on Thursday August 06, 2020 @04:18PM (#24075017)
Even HTTP/2 Push is an order of magnitude slower than local File I/O on a good day with perfect (internet) weather. ESM isn't even the first approach to try to deal with this, for instance AMD had nothing (at first) to do with "big money" and everything to do with average web developers trying their best to practically solve the module problem for the web and CommonJS was never going to be an option in a web browser (even if AMD or ESM spec authors had magic foresight to HTTP/2 or HTTP/3 they'd still have the same concerns with CommonJS).

CommonJS was a mistake that the JS world is going to live with for years more.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by z3t4 on Friday August 07, 2020 @02:51PM (#24085232)
The Node.js module system is only loosely based on CommonJS. The Node.js module system is probably the biggest reason of the success of the Node.js ecosystem. Ifyou want to attack Node.js you would start with the module system.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by wruza on Thursday August 06, 2020 @05:55AM (#24069542)
The last time I asked here why async-await is even a thing, when you can just introduce light/green threads, coroutines, you name it, everyone jumped that it is even better when you mark awaiting points explicitly. Now that it goes from a blog post of some popular language designer, seems no one gonna disagree.

>async computation mature over the years

A good thing, really, but would be much better if the experience from few decades ago wasn't ignored by cool kids who mature.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by gridlockd on Thursday August 06, 2020 @06:48AM (#24069777)
The reason why async/await is a good fit for Javascript is that it neatly wraps up asynchronous callback hell and Promises, which were already ubiquitous in Javascript code, because the web runtime enforces non-blocking IO.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by wruza on Thursday August 06, 2020 @07:32AM (#24070025)
It may be the reason, but not the only option.

  function cpsfoo(cb) {...}
  function foo() {
    var thread = this_thread()
    cpsfoo(x => thread.resume(x))
    return thread.yield()
  }
Error and immediate cb invokation handling omitted for clarity. Similar wrapper or wrapper-generator (uncps(f), unpromise(f)) may be done for other primitives.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by cookiengineer on Thursday August 06, 2020 @07:43AM (#24070072)
> Error and immediate cb invokation handling omitted for clarity.

... but this is exactly the reason why callback hell existed. Error handling down the callback branches were a total mess, especially with networked state transfers.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by wruza on Thursday August 06, 2020 @12:39PM (#24072630)
Error handling can be easily added to the example wrapper above, completely hiding all the burden from a user of foo(), who then could do their usual try-catch. It's just my phone keyboard that prevents me from implementing it right here.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by gridlockd on Thursday August 06, 2020 @08:45AM (#24070387)
Sure, it's not the only option, just the better option for Javascript. Your example isn't representative of Javascript.

This is an example for something that might come up in Javascript, using async/await:

    const a = await fetchA();
    const b = await fetchB(a);
    const stuff = await fetchStuff(a, b);
    const transformedStuff = await Promise.all(stuff.map(someAsyncTransform));
Error handling is just try/catch.

Note that you generally have no control over most of these function being asynchronous. Threads or coroutines do nothing to help you write straightforward code when all your libraries are based on fine-grained callbacks/promises. Writing out that chain of dependencies in CPS would be a nightmare.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by anonymoushn on Thursday August 06, 2020 @02:05PM (#24073720)
They really do though. In the modal case you would just write the same code as in js but without all the extra keywords. If you need fanout you'd call some special function to do that, like you do now.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by merb on Thursday August 06, 2020 @08:45AM (#24070386)
well coroutines are a way better option and working way better than promises, unfortunatly promises were introduced since it's easier to convert a callback function to a promise based one
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by spanhandler on Thursday August 06, 2020 @09:21AM (#24070603)
Never should have had to worry about that stuff to begin with, unless you were actively using it. I’ve read and written way more code that had to wrestle async back to sync than code that’s actually been able to let async happen at the local scope. Of course if you’re working in a framework (Express, say) one might expect that it would run your entire mostly-sync pile of code async, so it can hop over and process another request. And that’s fine. That’s how it should have worked. Most things should execute in an async context so other things can happen while they’re waiting on IO or whatever, but within them async behavior should have been what required a keyword or a bunch of hideous nesting or whatever.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by pansa2 on Thursday August 06, 2020 @07:12AM (#24069904)
Do Promises and async-await work together better than, say, Promises and Lua-like stackful coroutines?
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by WorldMaker on Thursday August 06, 2020 @04:38PM (#24075209)
This description is probably far more abstract than most would like (and it is probably wrong by not being exact enough, I am not a mathematician I am a software developer), but might be useful to some: coroutines are a join calculus and Promises are a monad. async/await is syntactic sugar for a monad (less general than Haskell's do-notation, more general than only Promises, though rarely in practice used much beyond Promises). You could plumb Promises on top of coroutines easily (and while JS engines are more traditionally event loops; other languages that support async/await such as Python use much more coroutine-like underbellies), and while you don't get the full "join calculus" power/flexibility by putting the monad abstraction on top of the join calculus, you get the benefit of monad transformers like async/await.

Those benefits being that the async/await syntax and semantics for working with Promises more closely resembles the imperative code it replaces, whereas the join calculus is its own [sometimes easy, admittedly] learning curve with its own syntax and semantics. Most of the complaints about Promises are the usual complaints about monadic wrapping types that the types become "viral" and "color" all associated code. But that just means that the failure cases are more easily picked up by a static type checker than semantic failures in learning the join calculus.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by z3t4 on Thursday August 06, 2020 @03:15PM (#24074420)
I dont know Lua, but async/await in JavaScript is syntax sugar for a coroutine.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by nemothekid on Thursday August 06, 2020 @10:59AM (#24071488)
In regards to JS, any reasons for a sync await seems to be rationalization.

Node is built on V8, which is built for browsers. Browsers have a single threaded execution model that also hides an event loop that runs the entire tab.

Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by dboreham on Thursday August 06, 2020 @12:09PM (#24072279)
Yes, async await is a bug, introduced to workaround the lack of proper concurrency in the runtime.
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by secondcoming on Thursday August 06, 2020 @04:42AM (#24069182)
The whole point of that article is that it hasn't really matured at all
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge... by dgellow on Thursday August 06, 2020 @04:56AM (#24069244)
I’m not sure if that’s true. I remember that at some point there were more than 2 systems, none were standards, and they were widely incompatible, I remember AMD for example: https://github.com/amdjs/amdjs-api/blob/master/AMD.md.

I cannot find the others though.