4 ms·
It may be better to ask yourself what does await do? I am no longer familiar enough with C#, so I will take NodeJS as an example. In NodeJS everything that yo
by tomjen3 5y ago
It may be better to ask yourself what does await do?
I am no longer familiar enough with C#, so I will take NodeJS as an example.
In NodeJS everything that you do happens on one thread, which is great because you will never, ever, need to worry about locks and threads.
The downside is that you can only serve one client at a time. To avoid this, whenever you start reading a file, or send a network package, or something else that takes a long time where you are not doing anything, Node stops running your code and runs some other code for a while. Then it will come back to you when it has a result and start running the code again.
It is easy to understand for us what come back means, but how to express that in an language? First attempt was with callbacks - node would come back to whatever you put in the callback. The downside of this is that you get very nested code, you have to repeat code to handle errors and it becomes a mess fast, especially if you have to deal with collections.
The next attempts are promises. These are a lot easier to deal with than callbacks, because you get some value back than you can treat like just another object (put in containers, etc) but in practice your code still ends up having to stop and pass values through functions all the time, which puts in visual noise and is a pain to deal with.
Next step then is async/await. Instead of coming back by executing a function, you use the keyword 'await' to tell the compiler where to await to. As for why this can't be used inside a function, await is a legal identifier in plain Javascript - await(someValue) - has a very different meaning if you have your function marked as async vs if you don't.
I use await a lot at work, and my mental model is just that it is the same as promises, except that instead of having to write an annoymous function, invent a new name for the result of the promise and showel my code into the anonymous function, the compiler does so for me.
Thus I get to write:
let a = await fs.readFile....
let dirContent = await fs.readDir(...
return dirContent.contains(a)
Instead of:
return fs.readFile....then(fileContent => fs.readDir(...).then(dirContent => dirContent.contains(a)))
Which is, to me, borderline unreadable.
- endorphine 5y ago> It may be better to ask yourself what does await do? Yes, probably that's something that slips me too. > It is easy to understand for us what come back means, but how to express that in an language? First attempt was with callbacks - node would come back to whatever you put in the callback. I feel like I have to even understand what NodeJS does when you give it a callback function. So, how the asynchronous model works in a pre-Promise, pre-async/await world? And then when the loop is notified from epoll that a response came back, it goes and executes that callback function with the data? Is that why they say that in async, the state is spilled all over the heap? Because the original function that issued the HTTP request and provided the callback, returned immediattely, and that callback function had to be allocated somewhere on the heap so that it could be executed eventually when that I/O was complete? Does NodeJS uses epoll to ask to be notified when a response from a TCP socket comes in, and then puts the callback function in some kind of internal queue (the one that the event loop consumes from?)?