Question

Recently in C# 4.0 the task based approach for async programming was unveiled. So we were trying to develop some of our functions which used callbacks earlier.

The problem we are facing is with the implementation of multiple responses for the functions using tasks. E.g. we have a function which fetches some data from a thirdparty API. But before fetching the data from API we first check whether we already have it in our in-memory cache or in DB then only we go to the API. The main client application sends a request for a list of symbols for which data to fetch. If we find data for some symbols in cache or in DB we send it immediately via the callback. For remaining symbols we request the API.

This gives a feeling of real-time processing on client application for some symbols. And for other symbols the user gets to know that it will take time. If I do not send responses to the client instantly and first collect all the data and then only send response for the whole list then the user will be stuck for 99 symbols even if only 1 symbol is to be fetched from API.

How can I send multiple responses using the task based approach?

Was it helpful?

Solution

It seems like you want to have an async method that returns more than once. The answer is you can't. What you can do is:

  1. Call 2 different methods with the same "symbols". The first only checks the cache and DB and returns what it can, and the second one only calls the remote API. This way you get what you can fast from a cache and the rest more slowly.
  2. Keep using callbacks as a "mini producer consumer" design so you can call it as many times you like.

I could try for a more concrete answer if you post the code you're using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top