>async / await
Why is it so hard? How do you make sense of this concept?
Shopping Cart Returner Shirt $21.68 |
Tip Your Landlord Shirt $21.68 |
Shopping Cart Returner Shirt $21.68 |
>async / await
Why is it so hard? How do you make sense of this concept?
Shopping Cart Returner Shirt $21.68 |
Tip Your Landlord Shirt $21.68 |
Shopping Cart Returner Shirt $21.68 |
its not, moron
It's not hard when you use the readily-made executor like everyone. Try making your own executor from a minimal coroutine API like C++.
>try reinventing the wheel!
why?
Because it's stupid. Just use a modern language with concurrency at it's core aka elixir
>aka Elixir
thanks but I'll stick with Clojure
Why not both?
https://lfe.io
Now you got me interested in Elixir. Thanks anon
> Just use a modern language with concurrency at it's core aka Golang
FTFY
I don't care, I just use use Fork::Manager;
>async: a function marked as async can use await, returns a promise
>await: enter the named async function, when it returns its promise (which may be right away or after several more levels of nested awaitz) put it on the end of a queue then loop through the queue until one of the promises resolves (I/O is finished). Return to the function that awaited it, await now "unwraps" the promise.
They are a way of combining coroutines, threads and promises with syntax that resembles normal code.
Just build your language to handle coroutines and threads then promises and your extra syntax is normal code.
What's the difference between threads and coroutines?
coroutines are single threaded aka threads for children
threads are an OS concept for a lighter-weight alternative to processes (they share memory and don't need to communicate via IPC) they're scheduled by the OS (and by the program if they're user threads)
to run two different threads in parallel you need two hardware cores, the OS and the hardware interweave them
coroutines are an abstraction for an operation that can (cooperatively) choose to pause itself and then be resumed
to run two coroutines at the same time (debatable if it is "in parallel") you don't need multiple threads or cores
basically: multiple threads are for things you can speed up by running more instructions, like computer graphics. (If you can) You divide the task between threads.
coroutines for things you CAN'T speed up by running more instructions, like I/O. You can have a 1THz processor but it's not going speed up a disk drive or a packet going through the network. You pause the coroutine instead of waiting and go do something else.
Instead of returning an X it returns a promise, from which at some point you can take out that X
Like a bank transfer, your account isn't frozen until your transfer goes through, the money is removed and you get a confirmation (a promise) that that money will soon arrive at the account you sent to.
>to run two different threads in parallel you need two hardware cores, the OS and the hardware interweave them
*if you want to run more than the amount of cores you have, the OS and the hardware will interweave them
Depends where you use it. In dotnet where it was used first and built for, it's as easy as naming a variable. In JS it's implemented in a very moronic and unintuitive way.
>spin up the threads
It isn't that hard, even in JS. Why is it so hard for you?
I just make every call synchronous - if the content doesn't load, well, then it ain't gonna load.
Me? I just use Task for everything.
People werent meant to think in the 4th dimension
>Hey Anon, can you clean the table why I prepare the food?
Yeah, concurrency is HARD, lmao.
More like can you clean the table unless I'm not cleaning and if I'm cleaning it mop the floor unless I'm moping the floor...
Only if you program in a moron language that is not meant for concurrency.
am i moronic or why does it feel so complicated? "returns a promise" what the hell is a promise?
an object that will hold the actual return value after the async function is done.
Well first you make a promise and later on you have to resolve it.
play a funny game called Bitburner, it uses this functionality in its core gameplay
> make synchronous call
> run for loop every second to check if the data is there
OH WOW, THAT WAS SOOOOO HARD, I NEED AWAIT/ASYNC SISTERS!
I tried making a multiplayer game in javascript and look at how it turned out
https://pastebin.com/KNpjFtRP
I'm never using nodejs as a backend ever again
You dont you just add await until it werks