I had a question regarding the working of co_await in C++. I have the following code snippet:-
// Downloads url to cache and
// returns cache file path.
future<path> cacheUrl(string url)
{
cout << "Downloading url.";
string text = co_await downloadAsync(url); // suspend coroutine
cout << "Saving in cache.";
path p = randomFileName();
co_await saveInCacheAsync(p, text); // suspend coroutine
co_return p;
}
int main(void) {
future<path> filePath = cacheUrl("https://localhost:808/");
return 0;
}
The co_await
keyword is used to suspend the execution of any co-routine. We have 2 instances in the above code where it is used. In the main function, we get access to the co-routine. When the program executes the line co_await downloadAsync(url)
will it invoke downloadAsync
or just suspend the co-routine.
Also, for executing the next saveInCacheAsync(p, text)
function, should the main function call resume on the co-routine ? Or will it get called automatically ?