title: How do promises guarantee that the 'resolve' function is executed after an asynchronous operation
body: <p>I have this code:</p>
  
  <pre><code>var myFirstPromise2 = new Promise((resolve, reject) =&gt; {
      //setTimeout models an operation that takes a long time
      setTimeout(function(){console.log('finishTimeout')},60000);
      resolve('success');
  });
  
  myFirstPromise2.then((successMessage) =&gt; {
      console.log('yay '+successMessage);
  });
  </code></pre>
  
  <p>The output is:</p>
  
  <pre class="lang-none prettyprint-override"><code>yay success 
  finishTimeout
  </code></pre>
  
  <p>I want <code>resolve()</code> to be executed after the lengthy operation is finished, so that the outputs are reversed.</p>
  
  <p>I am also confused about promises in general. The code above is no different from simply doing this:</p>
  
  <pre><code>setTimeout(function(){console.log('finishTimeout')},60000);
  console.log('yay success');
  </code></pre>
  
  <p><strong>EDIT:</strong> What is the difference between putting <code>resolve()</code> inside <code>setTimeout</code>, and just this:</p>
  
  <pre><code>setTimeout(function() {
      console.log('finishTimeout')
      console.log("yay success")
  },60000)
  </code></pre>