News for hackers. Stuff that matters.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by throwaway81523 on Friday April 08, 2022 @05:33AM (#30955037)
I feel like async/await is a huge antipattern in a language like JS. They should have gone for lightweight threads instead.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by samatman on Friday April 08, 2022 @07:24AM (#30955637)
I agree and claim the burden is on those who say "it's fine, actually" to rebut the definitive article[async:0] on the subject:

[await:0] https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... (read it whenever you would like, it's good I Promise).

If you've only ever used Javascript, it's tempting to think this is some fact of nature, rather than bad language design.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by johannes1234321 on Friday April 08, 2022 @07:50AM (#30955790)
While it is right, that the red/blue distinction is annoying at times it is the consequence out of the event-loop based approach created in 1995. Any other solution would have broken that more. It is assumed that the code we have is single threaded. That not being the case anymore leads to tons of concurrency issues. Right now I know that all my code, till the next await, will run (mostly) uninterrupted by external influence. No locking needed, nothing will change behind my back etc.

In a greenfield environment things certainly would look different, but unfortunately we are tied to past decisions till browser vendors decide to create an alternate universe (well, webassembly ...) and that has enough spread to rely on. But for that one would need agreement what that better future would be.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by anonymoushn on Friday April 08, 2022 @08:15AM (#30955955)
This isn't true. If you had stackful coroutines or something equivalent, you would be able to do all the things this thing does and you wouldn't have to pepper your callers and callers' callers with "await" whenever you add an operation that may take some time to a function for the first time.

We had this in Python and still have it in Lua, C, Zig, and so on, but the world has mostly gone the other way.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by samatman on Friday April 08, 2022 @08:32AM (#30956074)
I happen to be working on an application which is largely written in Lua and uses the same event loop (libuv) as Node.

I can confirm that with careful engineering, application code doesn't have to suffer from the color problem. Fundamentally the trick is to yield after creating a callback, and resume into that callback when the thread has more data to process. A `foo:bar():baz():bux()` chain can pause execution in any of those methods and the user doesn't have to know which (some knowledge of whether this will happen /at all/ is required, only because you need a coroutine since Lua can't yield the main thread).

This isn't built-in, need to start with something like BEAM for that, but it's a nice sweet spot.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by poink on Friday April 08, 2022 @09:41AM (#30956661)
I think the issue they were getting at is code like this:

    function doSomething(doSomethingElse) {
      internalState.setUp();
      doSomethingElse();
      internalState.tearDown();
    }
Right now, you can safely assume this whole thing will execute to completion without anything else happening in the interim, so a lot of existing JS makes that assumption. If `doSomethingElse` can yield while other code executes the assumption becomes invalid.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by laurencerowe on Friday April 08, 2022 @07:58AM (#30955848)
It's worth reading that excellent article alongside Glyph's excellent article from the opposite perspective: https://glyph.twistedmatrix.com/2014/02/unyielding.html

A few languages avoid the pitfalls of threads, like Rust where its ownership system protects you.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by anonymoushn on Friday April 08, 2022 @09:17AM (#30956436)
This is a nice post. Off the top of my head, I can't think of any ways of avoiding this issue. Even Rust doesn't really avoid it because different requests could plausibly create their own representations of the payee and payer.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by laurencerowe on Friday April 08, 2022 @10:14AM (#30957007)
I think Rust avoids this issue because it limits you to a single mutable reference. So even if each thread constructed its own payee and payer, their underlying balances would still reference the same shared state so only one thread at a time would be allowed a mutable reference to payee and payer balances.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by anonymoushn on Friday April 08, 2022 @11:15AM (#30958180)
It doesn't limit you to a single in-memory representation of each entry in a remote database.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by laurencerowe on Friday April 08, 2022 @01:02PM (#30959681)
> It doesn't limit you to a single in-memory representation of each entry in a remote database.

If you have a remote database then you likely have multiple processes at play. For safety in those cases you need to rely on transactional safety at the database level or if transactions are not available then you may want to use a tool like TLA+ to check your distributed algorithm.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by anonymoushn on Saturday April 09, 2022 @03:04AM (#30965818)
The example clearly has that though, otherwise it would not need

  yield from server.update_balances([payer, payee])
Re:You Can Throw() Anything in JavaScript – and Other Async/... by laurencerowe on Friday April 08, 2022 @12:31PM (#30959224)
(The above comment was changed. When I replied it was talking about different threads reading from a database.)

In that case you’d be relying on the database rather than the programming language to manage shared state. (The article is more about how to safely manage shared state within a process.)

Re:You Can Throw() Anything in JavaScript – and Other Async/... by IshKebab on Friday April 08, 2022 @12:33PM (#30959255)
You could probably do it for a key value store, but I think it would take a custom database to do it for a relational database.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by ThunderSizzle on Friday April 08, 2022 @08:07AM (#30955910)
I personally prefer async/await over callbacks or promises. I find the flow better when I can make an Ajax call just await the result, rather than having to implement a success function.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by paskozdilar on Friday April 08, 2022 @09:44AM (#30956691)
The "lightweight thread" alternative would probably make the Ajax call blocking, so you could just launch a thread that waits for it to finish, then continues on with the rest of its instructions. The "callback hell" wouldn't exist in the first place.

This is exactly what Golang did, and in my honest opinion, it's a breeze of fresh air in the modern mess of processes/threads/asyncawaitcoroutines and all their combinations.

Re:You Can Throw() Anything in JavaScript – and Other Async/... by junon on Friday April 08, 2022 @05:39AM (#30955081)
I actually quite like it. Matter of opinion.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by heavenlyblue on Friday April 08, 2022 @05:52AM (#30955149)
I love this thread, pseudonymous circle of unjustified likes or dislikes of a specific technology.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by creativenolo on Friday April 08, 2022 @06:31AM (#30955344)
I'm not sure I like this thread.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by isaacimagine on Friday April 08, 2022 @06:37AM (#30955370)
I love the metacommentary on this thread, in that it matches the pattern of original commentary.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by paskozdilar on Friday April 08, 2022 @06:54AM (#30955446)
I don't like the metacommentary on this thread so much. The fact that the first two commends are dislike-like and the rest are like-dislike really ruins it for me.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by mariusmarais on Friday April 08, 2022 @07:59AM (#30955859)
I like how the replies are starting to make the little arrow thing. Reminds me of something...
Re:You Can Throw() Anything in JavaScript – and Other Async/... by jdrc on Friday April 08, 2022 @05:54AM (#30955162)
Glorified Goto spaghetti
Re:You Can Throw() Anything in JavaScript – and Other Async/... by junon on Friday April 08, 2022 @09:05AM (#30956325)
Goto is super helpful in C for error handling. But yes, let's continue parroting the same old rhetoric.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by jdrc on Monday April 11, 2022 @01:56AM (#30985178)
is await better than goto? frankly i prefer the latter
Re:You Can Throw() Anything in JavaScript – and Other Async/... by smt88 on Friday April 08, 2022 @06:57AM (#30955472)
It is in some languages, but not in JS. It's definitely not good, but it's not goto spaghetti.
Re:You Can Throw() Anything in JavaScript – and Other Async/... by anonymoushn on Friday April 08, 2022 @08:20AM (#30955989)
goto is fine idk
Re:You Can Throw() Anything in JavaScript – and Other Async/... by __ryan__ on Friday April 08, 2022 @06:48AM (#30955422)
Why?