title: Some questions about Promise in JavaScript
body: <p>I am confused with the behavior of Promise in JavaScript and have some questions, could someone help me to figure them out?</p>
  
  <ol>
  <li><p>These are two common ways to create Promise.</p>
  
  <pre><code>function promise(){
      return new Promise((resolve, reject) =&gt; {
          resolve()});
  }
  
  let promise = new Promise(function(resolve, reject){
      resolve();
  });
  </code></pre>
  
  <p>The first function creates a Promise object but doesn't call it until we call the function. In comparison, the second statement creates a Promise object and call it immediately. Am I right?</p></li>
  <li><p>For this function:</p>
  
  <pre><code>function timeout(ms){
      return new Promise((resolve, reject) =&gt; {
          setTimeout(resolve, ms, 'done');
  });
  }
  
  timeout(100).then((value) =&gt; {
      console.log(value);
  });
  </code></pre>
  
  <p>I think when we call the timeout function, it firstly creates an asynchronous function <code>setTimeout</code> and pushes it at the tail of the event queue. After all synchronous events finished, it will be called and creates a Promise object. The Promise object will also be pushed at the tail of the event queue, which is after other synchronous events. So there seems to be two event loops, am I right?</p></li>
  <li><p>Are these two statements equal?</p>
  
  <pre><code>let promise = new Promise(function(resolve, reject){
      resolve();
  });
  
  let promise = Promise.resolve();
  </code></pre></li>
  <li><p>Why is the output sequence like the following?</p>
  
  <pre><code>setTimeout(function(){
      console.log('3');
  }, 0);
  
  Promise.resolve().then(function(){
      console.log('2');
  });
  
  console.log('1');
  
  // 1
  // 2
  // 3
  </code></pre>
  
  <p>That is an example in a book, the author explains as following: the <code>setTimeout(fn, 0)</code> will be called at the beginning of the next event loop, the <code>Promise.resolve()</code> will be called at the end of current event loop and the <code>console.log()</code> will be called immediately.</p>
  
  <p>I am confused with why the <code>setTimeout</code> function will be called at the beginning of the next event loop. I think it is also an asynchronous function and will be pushed at the end of current event queue, so it should be called before the Promise object. Could someone tell me why I am wrong.</p></li>
  </ol>