bdristan bdristan - 2 months ago 19
reST (reStructuredText) Question

What should last task in a chain return - c++ rest sdk?

In the following code, why is variable

t
assigned a correct task when neither of lambdas in the chain return such a type? The lambdas return
task<http_response>
,
task<json::value>
and none.

The code works when the last lambda returns task and when it returns nothing.

Any pointers to online docs that shed some light on it will be appreciated.

Thanks.

pplx::task<void> t = client.request(methods::GET, L"/api/dummy")
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
response.content_ready().wait();
return response.extract_json(); // return task<json::value>
}
})

.then([](json::value value)
{
if (value.has_field(L"id"))
{
int id = value.at(L"id").as_integer();
}

// t.wait() below works regardless if this
// line is commented out or not.
//return pplx::task<void>({});
});

try
{
t.wait();
}
catch (const std::exception &e)
{
[...]
}

Answer

then always returns some kind of task, take a look here. if the callback supplied to then does not return anything then will just return task<void>. in a nut shell, then will return a task that correspond to the return type of the callable supplied to then

task<T> t1 = t0.then([]{ return T(); });
task<void> t2 = t3.then([]{ /*return nothing*/ });

let's break this to parts:

task<http_response> t0 = client.request(methods::GET, L"/api/dummy");
task<json::value value> t1 = t0.then(/*return json from response*/);
task<void> t = t1.then(/*extract "id", returns nothing*/);
t.wait();

now, take a look on what happens when you use co_await:

 auto response = co_await client.request(methods::GET, L"/api/dummy");
  if (response.status_code() == status_codes::OK)
    {
        response.content_ready().wait();
        auto value = co_await response.extract_json();
        if (value.has_field(L"id"))
        {
           int id = value.at(L"id").as_integer();
        }
    }

isn't it neat? the entire continuation chaining just collapsed to synchronous code, but the code is still asynchronous behind the scenes!

Comments