title: Executing 3 asynchronous operations (where the 2nd one depends of a condition)
body: <p>I am working on a <strong>durandal / breeze</strong> project.</p>
  
  <p>I need to perform several things in my activate function. Thanks to <code>promises</code> I am able to chain asynchronous operations. Please note that I'm still a beginner with all these things so don't hesitate to correct my code.</p>
  
  <p>I have the following activate function inside my viewModel:</p>
  
  <pre><code>var activate = function (routeData) {
  
      initLookups()
  
      var idTran = parseInt(routeData.idTran);
      var idItin = parseInt(routeData.idItin);
  
      return datacontext.getTransportById(idTran, transport)
              .then(function () { return datacontext.getItineraryById(idItin, itinerary); });
  }
  </code></pre>
  
  <p><strong>So in the return statement:</strong></p>
  
  <ol>
  <li><p>I fill the <code>transport</code> observable thanks to <code>getTransportById</code></p></li>
  <li><p><em>then</em> I fill the <code>itinerary</code> observable thanks to <code>getItineraryById</code></p></li>
  </ol>
  
  <p>-</p>
  
  <p>So far so good. This code works and do his job as expected (maybe not optimised). Now I need to insert a condition between these 2 operations for the case of creating a new entity (if idItin==-1). In this case, a new entity is created. I try with the code below but it doesn't work: I mean the <code>promise</code> doesn't seems to do his job here and thus the view is displayed without waiting for the asynchronous operations to complete.</p>
  
  <pre><code>var activate = function (routeData) {
  
      initLookups()
  
      var idTran = parseInt(routeData.idTran);
      var idItin = parseInt(routeData.idItin);
  
      return datacontext.getTransportById(idTran, transport)
              .then(function () { if (idItin == -1) return datacontext.createItineraryDetailTransport(idTran); })
              .then(function () { return datacontext.getItineraryById(idItin, itinerary); });
  }
  </code></pre>
  
  <p>And below is the <code>createItineraryDetailTransport</code> function of the datacontext:</p>
  
  <pre><code>var createItineraryDetailTransport = function (idTransport) {
  
      var initialValues = ({
          transportId: idTransport
      });
  
      var newItinerary = manager.createEntity(entityNames.itinerary, initialValues);
      return manager.addEntity(newItinerary);
  }
  </code></pre>
  
  <p>So does someone have an idea how to code this thing? </p>