title: Sequential execution of async operations in Android
body: <p>Sequential execution of asynchronous operations in Android is at least complicated.
  Sequential execution that used to be a semi-colon between two operators like in <code>do_this(); do_that()</code> now requires chaining listeners, which is ugly and barely readable.</p>
  
  <p>Oddly enough, the examples that demonstrate the need for chaining sequential operations usually look contrived, but today I found a perfectly reasonable one.</p>
  
  <p>In Android there is in-app billing, an application can support multiple so-called in-app products (also known as SKU = stock keeping unit), letting the user, for example, buy (pay for) only the functionality that he/she needs (and, alas, also letting bearded men sell bitmaps to teenagers).</p>
  
  <p>The function that retrieves in-app product info is</p>
  
  <pre><code>public void queryInventoryAsync(final boolean querySkuDetails,
                                  final List&lt;String&gt; moreSkus,
                                  final QueryInventoryFinishedListener listener)
  </code></pre>
  
  <p>and it has a restriction that the list must contain at most 20 items. <em>(Yes it does.)</em>
  Even if only a few of these 20 are registered as in-app products.</p>
  
  <p>I want to retrieve, say, information about one hundred in-app products. The first thought would be to invoke this function in a loop, but only one asynchronous operation with the market is allowed at any moment.</p>
  
  <p>One may of course say "do not reuse, change the source", and even provide very good arguments for that, and this is probably what I will finally do, but I write this because I want see an elegant reuse solution.</p>
  
  <p><strong><em>Is there an elegant (=not cumbersome) pattern or trick that allows to chain several asynchronous operations in the general case?</em></strong></p>
  
  <p>(I underline that the asynchronous operation that uses a listener is pre-existing code.)</p>
  
  <p>UPD this is what is called "callback hell" ( <a href="http://callbackhell.com/" rel="nofollow noreferrer">http://callbackhell.com/</a> ) in the JavaScript world.</p>