JavaScript Promises and setTimeout
<p>I have been playing with Promises, but I am having trouble understanding what is happening with the following code:</p>
<pre><code>function a() { 
    return new Promise(function (resolve, reject) { 
        resolve(&quot;hi from a!&quot;);
    });
}

function b() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve(&quot;hi from b!&quot;);
        }, 5000);
    });
}

function c() { 
    return new Promise(function (resolve, reject) { 
        setTimeout(function () { 
            resolve(&quot;hi from c!&quot;);
        }, 1000);
    });
}

a().then(function (resultFromA) {
    console.log(&quot;a() responded with: &quot; + resultFromA);
    b();
}).then(function (resultFromB) { 
    console.log(&quot;b() responded with: &quot; + resultFromB);
    c();
}).then(function (resultFromC) { 
    console.log(&quot;c() responded with: &quot; + resultFromC);
});
</code></pre>
<p>I am expecting this to output <code>a() responded with: hi from a!</code> immediately, along with <code>b() responded with: hi from b!</code> and <code>c() responded with: hi from c!</code> after their respective <code>setTimeout()</code> fires. However, what I get this the following output immediately:</p>
<blockquote>
<p>a() responded with: hi from a!</p>
<p>b() responded with: undefined</p>
<p>c() responded with: undefined</p>
</blockquote>
<p>I was thinking that <code>.then()</code> waits on these promises, but it isn't. Any help would be appreciated.</p>