I was trying out System.Json (Beta) from NuGet. Also, trying to understand this new async/await stuff, just started Tinkering with Visual Studio 2012.
Wondering if using a ContinueWith if the await blocks until the whole thing is complete?
E.g, is this:
JsonValue json = await response.Content.ReadAsStringAsync().ContinueWith<JsonValue>(respTask => JsonValue.Parse(respTask.Result));
The same as:
string respTask = await response.Content.ReadAsStringAsync();
JsonValue json = await Task.Factory.StartNew<JsonValue>(() => JsonValue.Parse(respTask));
?
Those are similar but not identical.
ContinueWithreturns aTaskrepresenting the continuation. So, to take your example:Consider just the expression:
The result of this expression is a
Taskrepresenting the continuation scheduled byContinueWith.So, when you
awaitthat expression:You are indeed
awaiting theTaskreturned byContinueWith, and the assignment to thejsonvariable will not take place until theContinueWithcontinuation has completed:Generally speaking, I avoid
ContinueWithwhen writingasynccode. There’s nothing wrong with it, but it’s a bit low-level and the syntax is more awkward.In your case, I would do something like this:
I would also use
ConfigureAwait(false)if this were part of a data access layer, but since you’re accessingresponse.Contentdirectly I assume that you’ll need the ASP.NET context later in this method.Since you’re new to
async/await, you may find myasync/awaitintro helpful.