eekee wrote:
I'm a little confused on what async IO actually is, especially when reading. After requesting to read some data, do you poll for it later, or is it delivered by callback, or what?
Any of that.
It depends on the application.
For something like a single threaded game loop, you could use a message when the data is ready, and process in your gameloop once it's ready (that way the rest of the game can continue running while your data is being read.)
Callbacks are nice, anonymous functions can help with readability by putting the handling code right next to the calling code, but you could end up in nested hell pretty quickly.
Code:
ReadFile(..., (data) => {
WriteFile(..., () => {
ReadFile(..., (data) => {
});
});
});
A+ promises are better, because you can chain promises together, e.g.
Code:
ReadFile(...).then((data) => {
return WriteFile(..);
}).then(() => {
return ReadFile();
}).then((data) => {
});
And you often have functions for joining promises (anyof, allof, etc.)
Await/async can often make this code cleaner and read more sequentially, which can be nice because it makes it easy to do loops and the such:
Code:
data = await ReadFile(...);
await WriteFile(...);
data = await ReadFile(...);
Or you could use a future, which executes the code in another thread/fiber, the waits for it to finish executing when you call .get():
Code:
future_data = MakeFuture(() => {
data = ReadFile(...);
WriteFile(...);
return ReadFile(...);
});
....
data = future_data.get();