dlmelendez/identitydocumentdb

better async code ?

Closed this issue · 1 comments

I don't quite understand why the async code is written in this way: Start a new task, then block that task in waiting.

e.g. Current code:

        public async virtual Task CreateAsync(TUser user)
        {
    ......
            await new TaskFactory().StartNew(() =>
            {
                var docTask = Context.Client.CreateDocumentAsync(Context.UserDocumentCollection.DocumentsLink, user
                    , Context.RequestOptions, true);
                docTask.Wait();
                var doc = docTask.Result;
                Context.SetSessionTokenIfEmpty(doc.SessionToken);
                JsonConvert.PopulateObject(doc.Resource.ToString(), user);
            });

        }

Can it be simplified as below code?

        public async virtual Task CreateAsync(TUser user)
        {
    ......
            var response = await Context.Client.CreateDocumentAsync(Context.UserDocumentCollection.DocumentsLink, user
                    , Context.RequestOptions, true);

            Context.SetSessionTokenIfEmpty(response.SessionToken);
        }

The latest release has many threading cleanup fixes including this one. Thanks..