title: Optional Promise API
body: <p>I have a validation library that is purely synchronous but is often used as part of a chain of async functions.  However, I have to maintain an existing synchronous API and would like to make the promise API optional.  </p>
  
  <p><strong>Can I somehow detect (at runtime) whether a function is part of a Promise chain?</strong></p>
  
  <p>This is pretty easy with callbacks since you can just check if a callback was passed in.  I understand that I could pass in an optional promise boolean, but that seems inelegant.</p>
  
  <p>I've also considered doing a callback interface and using a library to convert the callback interface to a promise based interface on the fly.  However, I'm working in Haxe and I would prefer to keep the transforms/abstractions down to a minimum.</p>
  
  <p>I also understand that you can sandwich regular functions in-between promises, but there are some instances where the behavior would differ between the two.</p>
  
  <p><strong>FINAL Edit</strong> Lots of confusion as to why I can't just return the same values, first example (below) didn't seem to help.  Remember, this is still simplified.</p>
  
  <pre><code>//mix of sync with promise 
  new Promise(function(resolve, reject){
    var safeToAdd = thingTracker.preflight(newThing);
    if(safeToAdd){
      return client.request.addThing(newThing); //send request to server
    } else {
      reject(newThing.errorMessages); //requires explicit reject, cannot just pass results along
    }
  }).then(function(newThing){ //client and server both cool with newThing?
    thingTracker.save(newThing);
  }).catch(function(errorMessages){ //handles errorMessages from client and server
    ui.show(errorMessages);
  });
  
  //pure promise
  thingTracker.preflight(newThing).then(function(){
    return client.request.addThing(newThing); //sends request to server
  }).then(function(newThing){  //client and server both cool with newThing?
    thingTracker.save(newThing);
  }).catch(function(errorMessages){ //handles errorMessages from client and server
    ui.show(errorMessages);
  });
  </code></pre>
  
  <p>(Old) <strong>Edit</strong> to clarify (but didn't really):</p>
  
  <pre><code>function preflight(thing){
    var validity = thing === 42;
  
    if(promise){
      if(validity){
        return Promise.resolve(validity);
      } else {
        return Promise.reject(validity);
      }
    } else {
      return validity;
    }
  }
  </code></pre>
  
  <p>Obviously I can do the same checks in a <code>then</code>'s anon function, but that is not much better than using the sync interface directly.  Also note that this is a very simple example, in the real function there are side effects on <code>thing</code> and messages are produced.</p>
  
  <p><strong>Edit</strong> Just to illustrate my point a bit better, here is a <a href="https://gist.github.com/indolering/963091dcda47b6d008d0" rel="nofollow noreferrer">gist</a> of what things look like.</p>