title: Is there any way to resolve a promise synchronously? (or an alternative library that can)
body: <p>I have a method for validating a string, I want that method to return a Promise as the validations being ran <em>may</em> be asynchronous. The issue I am having however is one of performance, I want the promise to resolve in the same event loop when possible (eg: when there are no asynchronous validations to be done) but I want the interface to remain consistent (eg: to always return a Promise).</p>
  
  <p>The simplified code example below illustrates what I'm trying to do, but it incurs the aforementioned performance penalties because even when the validation can be performed synchronously it still waits for the next event loop to process the result.</p>
  
  <p>In my specific use case this performance penalty is too high.</p>
  
  <p>Below is a simplified (minimal) example of what I'm doing</p>
  
  <pre><code>// Array containing validation methods
  const validations = [
    (value) =&gt; true, // Some validation would happen here
  ];
  // Array containing asynchronous validation methods
  const asyncValidations = []; // No async validations (but there could be)
  const validate(value){
    // Run synchronous validations
    try {
      validations.forEach(validation =&gt; validation(value));
    catch(error){
      // Synchronous validation failed
      return Promise.reject();
    }
    if(asyncValidations){
      return Promise.all(asyncValidations.map(validation =&gt; validation(value));
    }
    // Otherwise return a resolved promise (to provide a consistent interface)
    return Promise.resolve(); // Synchronous validation passed 
  }
  
  // Example call
  validate('test').then(() =&gt; {
    // Always asynchronously called
  });
  
  </code></pre>