I'm worked on a CLIENT SERVER application with cpprestsdk and it works fine. Now i want to improve readability of my code (lots of .then() method) using co-routines. Using co_await the compiler,(on VC++2015 or VC++2017) will refactor the code in a "continuation" getting rid of .then statement. I try to undestand how co_await works but actually i don't have complete understanding. Generally speaking if i have a task and a continuation like this:
client1.request(methods::GET, addr).then([=](http_response response)
{
printf("Response status code %u returned.\n", response.status_code());
if (response.status_code() == status_codes::OK)
{
wstring output = response.extract_utf16string().get();
wcout << output << endl;
}
}).wait();
it becomes easily
auto response = co_await client1.request(methods::GET, addr);
printf("Response status code %u returned.\n", response.status_code());
if (response.status_code() == status_codes::OK)
{ wstring output = response.extract_utf16string().get();
wcout << output << endl;
}
The problem for me now is when i have cose like this:
pplx::task UploadFileToHttpServerAsync()
{
using concurrency::streams::file_stream;
using concurrency::streams::basic_istream;
namespace some_namespace = web::http;
// Open stream to file.
wstring filePath = L"out.txt";
return file_stream<unsigned char>::open_istream(filePath).then([=](pplx::task<basic_istream<unsigned char>> previousTask)
{
try
{
auto fileStream = previousTask.get();
// Make HTTP request with the file stream as the body.
http_client client(XMLReader::data.URL);
return client.request(methods::POST, filePath, fileStream).then([fileStream](pplx::task<http_response> previousTask)
{
How to use co_await to refactor my code if i have return statement?