title: How to conditionally run a code asynchonously using tasks
body: <p>I have a class in charge of retrieving resources which also caches them for quick access.
  The class exposes an asynchronous method for retrieving a resource:</p>
  
  <pre><code>public Task&lt;object&gt; GetResourceAsync(string resourceName)
  {
      return Task.Factory.StartNew&lt;object&gt;(() =&gt;
      {
          // look in cache
  
          // if not found, get from disk
  
          // return resource
      });
  }
  </code></pre>
  
  <p>The client code then looks like this:</p>
  
  <pre><code>myResourceProvider.GetResourceAsync("myResource")
      .ContinueWith&lt;object&gt;(t =&gt; Console.WriteLine("Got resource " + t.Result.ToString()));
  </code></pre>
  
  <p>This way, a background thread is always used. However, I don't want the code to run asynchronously if the object was found in the cache.
  If it was found in the cache, I'd like to immediately return the resource and not to have to use another thread.</p>
  
  <p>Thanks.</p>