title: Synchronize async actions with RxJS
body: <p>I want to understand more of the RxJS concept. Here is the problem I am currently struggling with. I want to abstract the accesses to a DB with async calls and i want to synchronize the accesses.</p>
  
  <p>Can i have a stream of actions,<br>
   - that do async calls to db<br>
   - i want the next action be delayed until the previous action is completed<br>
   - the caller of the action shall get an Observable for the result of the async actions.</p>
  
  <p>Example:  </p>
  
  <p>User of class calls Action1: Read DB item, calculate next state (e.g. increment field), write to DB<br>
  then..<br>
  User of class calls next actions (Action2), but Action1 is still in progress.<br>
  Action2: Read DB (shall not be started before the Action1:write is finished)  </p>
  
  <p>How can this be done with RxJS+Typescript?</p>
  
  <p>Frank</p>
  
  <p>/////////////////////////////////</p>
  
  <p>Meanwhile i have this code:</p>
  
  <pre><code>import * as Rx from 'rxjs';
  
  var actionQueue = new Rx.Subject&lt; () =&gt; Rx.Observable&lt;any&gt;&gt;();
  actionQueue
    .concatMap( v =&gt; v() )
    .subscribe( v =&gt; {});
  
  // example action with result type number
  function action1 ( v : number ) : Rx.Observable&lt;number&gt; {
    console.log( ':: action1: ', v );
    var res = new Rx.Subject&lt;number&gt;();
    actionQueue.next( () =&gt; {
      console.log( '&gt;&gt; action1: ', v );
      setTimeout( ()=&gt;{
        console.log( '&lt;&lt; action1: ', v );
        res.next(v);
        res.complete();
      }, 500 );
      return res;
    });
    return res;
  }
  
  // some actions enqueue now, after 700+2500ms
  action1( 11 ).subscribe( v =&gt; console.log( 'XX action1: ', v ));
  action1( 22 ).subscribe( v =&gt; console.log( 'XX action1: ', v ));
  action1( 33 ).subscribe( v =&gt; console.log( 'XX action1: ', v ));
  
  setTimeout( ()=&gt;{
    action1( 44 ).subscribe( v =&gt; console.log( 'XX action1: ', v ));
  }, 700 );
  
  setTimeout( ()=&gt;{
    action1( 55 ).subscribe( v =&gt; console.log( 'XX action1: ', v ));
  }, 2500 );
  </code></pre>
  
  <p>The output shows that it does sequential stuff.<br>
  As typescript/js noob... is this code having pitfalls? Is there a more elegant way?</p>
  
  <p>Frank</p>