this post was submitted on 28 Jan 2024
675 points (94.8% liked)

Programmer Humor

31968 readers
560 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] snowe 102 points 7 months ago (7 children)

This is my favorite version of this so far.

[–] [email protected] 33 points 7 months ago

I'm gett the ing UDP same vib joke

load more comments (6 replies)
[–] [email protected] 21 points 7 months ago (2 children)
[–] [email protected] 157 points 7 months ago (2 children)

I PROMISE the rest will come eventually

[–] [email protected] 48 points 7 months ago

that is going to take some RESOLVE

[–] [email protected] 14 points 7 months ago (1 children)
[–] [email protected] 3 points 7 months ago

Async are lazy loaded by design in rust, so... Yeah

[–] [email protected] 7 points 7 months ago

You must .await before you can have the Result. Then you can use ?

[–] [email protected] 15 points 7 months ago

For those who don't get it, it's the "stop doing science" meme.

[–] [email protected] 8 points 7 months ago* (last edited 7 months ago)

I'm actually laughing out loud at the amount of whitespace because ~~thread~~ routine is still executing lmao

[–] [email protected] 7 points 7 months ago (11 children)

I honestly don't know, why async got so popular. It seems like the entire reason for doing it was that some blokes were hellbent on using JS on a server and couldn't fathom that a second thread might be a good idea.

[–] [email protected] 82 points 7 months ago (1 children)

If you are waiting for IO, why would you block your current thread and not let it do something else? Async does not only exist in JS.

load more comments (1 replies)
[–] [email protected] 48 points 7 months ago

After using both extensively I would argue async code is easier to read. It has a lot less nesting. And generally easier to read code is a good thing so I'm all for async.

[–] Lmaydev 40 points 7 months ago* (last edited 7 months ago) (2 children)

A huge amount of time in apps is spent waiting for IO, database or web requests to complete.

Async prevents locking a thread during this wait.

If you're handling a large amount of requests in a web server, for example, it allows other requests to progress while waiting for these operations.

Threads are also expensive to start and manage.

Also handling threads manually is a pain in the ass.

[–] [email protected] 3 points 7 months ago (5 children)

Async prevents locking a thread during this wait.

That's a very common misconception. async is just a scheduling tool that runs at the end of event loop (microtask queue). It still runs on the main thread and you can still lock up your UI. You'd need Web Workers for actual multi-threading.

load more comments (5 replies)
[–] [email protected] 1 points 7 months ago (1 children)

If you need to get multiple pieces of data for one request Async is great, but why would you work on different requests in the same thread? Why slow down one request because the other needs a bunch of computation?

[–] Lmaydev 3 points 7 months ago

You aren't slowing down anything. If you didn't use async that thread would be blocked.

You'd need a thread per request even though they are sat doing nothing while waiting for responses.

Instead when you hit an await that thread is freed for other work and when the wait is over the rest of the code is scheduled to run.

[–] [email protected] 27 points 7 months ago

Because the alternative is a series of ridiculously nested call backs that make code hard to read and manage?

I honestly can't fathom how anyone would dislike async programming.

[–] [email protected] 25 points 7 months ago (12 children)

Async is good because threads are expensive, might aswell do something else when you need to wait for something anyways.
But only having async and no other thread when you need some computation is obviously awful.. (or when starting anothe rthread is not easily manageable)

Thats why i like go, you just tell it you want to run something in parallel and he will manage the rest.. computational work, shift current work to new thread.. just waiting for IO, async.

load more comments (12 replies)
[–] [email protected] 15 points 7 months ago (1 children)

Ok, I'm a c# developer and I use async await quite extensively. Is it different in JS? Or am I missing something?

[–] [email protected] 16 points 7 months ago

Nah, they're very similar, really. You generally kick IO heavy stuff you don't need immediately off to async await.

There are a few more applications of it in C# since you don't have the "single thread" to work with like in JS. And the actual implementation under the hood is different, sure. But conceptually they're similar. Pretty sure JS was heavily influenced by C#'s implementation and syntax.

[–] [email protected] 10 points 7 months ago* (last edited 7 months ago)

Imagine a webser or proxy and for every incoming request it creates an new thread 💣

Yes you're right if it's a second or third thread it is/may be fine. But if you're i/o bound and your application has to manage quite a lot of that stuff in parallel, there is no way around delegating that workload to the kernel's event loop. Async/Await is just a very convenient abstraction of that Ressource.

[–] [email protected] 10 points 7 months ago* (last edited 7 months ago) (4 children)

Async rust with the Tokio Framework is pretty cool. Need none of that JS bloat for async.

[–] [email protected] 3 points 7 months ago (2 children)

Honestly I can't wrap my head how to effectively put computation into a thread, even with Tokio.

All I want is something like rayon where you got a task queue and you just yeet tasks into a free thread, and await when you actually need it

Might be too much JS/TS influence on me, or that I can't find a tutorial that would explain in a way that clicks for me

[–] [email protected] 3 points 7 months ago

Tokio specifically says not to use it for CPU intensive tasks and rayon would be better for this: https://tokio.rs/tokio/tutorial

Speeding up CPU-bound computations by running them in parallel on several threads. Tokio is designed for IO-bound applications where each individual task spends most of its time waiting for IO. If the only thing your application does is run computations in parallel, you should be using rayon. That said, it is still possible to "mix & match" if you need to do both. See this blog post for a practical example

[–] [email protected] 1 points 7 months ago

Tokio is for concurrency, not parallelism. Use it for IO stuff. They say rayon is good for that, but I haven't used that. If you just want something simple, I'd recommend working with threadpool.

load more comments (3 replies)
[–] [email protected] 9 points 7 months ago* (last edited 7 months ago)

async/await is just callback() and queueMicrotask wrapped up into a neat package. It's not supposed to replace multi-threading and confusing it for such is dangerous since you can still stall your main/UI thread with Promises (which async also wraps).

(async and await are also technically different things, but for the sake of simplicity here, consider them a pair.)

load more comments (1 replies)
[–] [email protected] 5 points 7 months ago (1 children)

async/await could be useful if one creates an entire program designed on and for it, but it stops being acceptable as soon as I need two .thens inside of each other because I tried to use fetch in a non-async app without race conditions

[–] [email protected] 5 points 7 months ago

Observables are your friends

[–] [email protected] 4 points 7 months ago (1 children)

Honestly, I don't get it.

Is it about the syntax sugar? Would you like to use callbacks instead?

Async programming is when you achive concurrency even with one thread. It's needed. There's no alternative to this.

[–] [email protected] 5 points 7 months ago (1 children)
load more comments (1 replies)
[–] [email protected] 2 points 7 months ago

We're here asyncronously waiting for the rest of the meme.

load more comments
view more: next ›