c# - Await or Task.FromResult -


i have 1 service lets say,

public interface isomeservice {     task<bool> dosomeexpensivecheckasync(string parameter); } 

and have class consume service. needs simple null checks , return service response back.

public class someserviceconsumer {     private readonly isomeservice _serviceclient;      public someserviceconsumer(isomeservice serviceclient)     {         _serviceclient = serviceclient;     }      public async task<bool> dosomething1async(string someparameter)     {         if (string.isnullorwhitespace(someparameter))         {             return false;         }         return await _serviceclient.dosomeexpensivecheckasync(someparameter);     }      //no async or await keywords        public task<bool> dosomething2async(string someparameter)     {         if (string.isnullorwhitespace(someparameter))         {             return task.fromresult(false);         }         return _serviceclient.dosomeexpensivecheckasync(someparameter);     } } 

should dosomething1async or dosomething2async?

according this answer, should not wrap unnecessary await have use task.fromresult(false) shortcircuiting in dosomething2async

but according this answer there cases try/catch , using statements should await before returning.

am correct in saying then, that

  1. if have use try/catch or using should await

  2. otherwise not await if going return. , use task.fromresult short circuiting

i dosomething1async more, , want everywhere if says doesnt matter :).

if you're worried it, cache task:

static readonly task<bool> falsetask = task.fromresult(false); 

the async keyword wraps exceptions in returned task, along proper stack trace. it's tradeoff, safety of behavior perf.

lets @ difference scenarios each different:

async task usesomething1async(string someparameter) {     // if isnullorwhitespace throws exception, wrapped in     // task , not thrown here.     task t1 = dosomething1async(someparameter);      // rather, it'll thrown here. best practice,     // it's users of task-returning methods expect.     await t1;      // if isnullorwhitespace throws exception,     // thrown here. users not expect this.     task t2 = dosomething2async(someparameter);      // never have been reached.     await t2; } 

just illustrating point here -- isnullorwhitespace not throw exceptions reason.

as far stack traces go, async stack traces determined await. no await means method disappear stack trace.

say dosomeexpensivecheckasync throws exception. in case of dosomething1async, stack trace caller -> dosomething1async -> dosomeexpensivecheckasync.

in case of dosomething2async, stack trace caller -> dosomeexpensivecheckasync. depending on complexity of code, can make things difficult debug.

in practice, directly return task if knew no exceptions thrown prior it, , if method name merely overload forwarding overload. there exceptions rule, there bound places want maximize performance. pick , choose carefully, realize might making life of , user harder.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -