Understanding JavaScript promise object
<p>I am trying to wrap my head around promise object in JavaScript. So here I have this little piece of code. I have a <code>promise</code> object and two <code>console.log()</code> on either side of the promise object. I thought it would print</p>
<blockquote>
<p>hi</p>
<p>There!</p>
<p>zami</p>
</blockquote>
<p>but it printed</p>
<pre><code>hi

zami

There!
</code></pre>
<p>Why it is like that? I have zero understanding on how promise works, but I understand how asynchronous callback works in JavaScript. Can any one shed some light on this topic?</p>
<pre><code>console.log('hi');
var myPromise = new Promise(function (resolve, reject) {
    if (true) {
        resolve('There!');
    } else {
        reject('Aww, didn\'t work.');
    }
});

myPromise.then(function (result) {
    // Resolve callback.
    console.log(result); 
}, function (result) {
    // Reject callback.
    console.error(result);
});
console.log('zami');
</code></pre>