Skip to content

Instantly share code, notes, and snippets.

@markpapadakis
Created January 9, 2020 19:49
Show Gist options
  • Save markpapadakis/073cf8b6c2ad89b60d76774bdece3252 to your computer and use it in GitHub Desktop.
Save markpapadakis/073cf8b6c2ad89b60d76774bdece3252 to your computer and use it in GitHub Desktop.
// contrived example
task<int> fun_with_coroutines() {
auto fd = co_await async_open("/tmp/file.txt", O_RDONLY);
if (-1 == fd) {
co_return - 1;
}
char buf[16];
if (const auto res = co_await async_read(fd, buf, sizeof(buf)); res != sizeof(buf)) {
close(fd);
co_return - 1;
}
close(fd);
try {
co_await transmit_junk(co_await async_connect("localhost:5121"), buf, sizeof(buf));
} catch (...) {
co_return - 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment