文章博客

技术团队文档示例


20191115-异步

<h5>1.异步笔试题</h5> <pre><code class="language-javascript">async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { console.log('async2'); } console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0) async1(); new Promise(function(resolve) { console.log('promise1'); resolve(); }).then(function() { console.log('promise2'); }) console.log('script end'); //script start =&gt; // 同步 //async1 start =&gt; //异步1-第一步 //async2 =&gt; //异步2-第一步 //=&gt;promise1 =&gt; //异步3-第一步 //=&gt;script end =&gt; //同步 //=&gt;async1 end =&gt; //异步1-第二步 //=&gt;promise2 =&gt; //异步2-第二步 //=&gt;setTimeout =&gt; //setTimeOut 回调 </code></pre> <h5>2.异步第二题</h5> <pre><code class="language-javascript">async function async1() { console.log('async1 start'); await async2(); console.log('async1 end'); } async function async2() { //async2做出如下更改: new Promise(function(resolve) { console.log('promise1'); resolve(); }).then(function() { console.log('promise2'); }); } console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0) async1(); new Promise(function(resolve) { console.log('promise3'); resolve(); }).then(function() { console.log('promise4'); }); console.log('script end'); //1. 'script start' //2. 'async1 start' //3. 'promise1' //4. 'promise3' //5. 'script end' //6. 'promise2' //7. 'async1 end' //8. 'promise4' //9. 'script end'</code></pre> <h5>3.异步第三题</h5> <pre><code class="language-javascript">async function async1() { console.log('async1 start'); await async2(); //更改如下: setTimeout(function() { console.log('setTimeout1') },0) } async function async2() { //更改如下: setTimeout(function() { console.log('setTimeout2') },0) } console.log('script start'); setTimeout(function() { console.log('setTimeout3'); }, 0) async1(); new Promise(function(resolve) { console.log('promise1'); resolve(); }).then(function() { console.log('promise2'); }); console.log('script end'); //1.'script start' //2.'async1 start' //3.'promise1' //4.'script end' //5.'promise2' //6.'setTimeout3' //7.'setTimeout2' //8.'setTimeout1' //提示:在输出为promise2之后,接下来会按照加入setTimeout队列的顺序来依次输出,通过代码我们可以看到加入顺序为3 2 1,所以会按3,2,1的顺序来输出。</code></pre> <h5>4.异步第四题</h5> <pre><code class="language-javascript">async function a1 () { console.log('a1 start') await a2() console.log('a1 end') //执行结束 } async function a2 () { console.log('a2') } console.log('script start') setTimeout(() =&gt; { console.log('setTimeout') }, 0) Promise.resolve().then(() =&gt; { console.log('promise1') //异步直接执行第一步 }) a1() let promise2 = new Promise((resolve) =&gt; { resolve('promise2.then') console.log('promise2') //直接执行 }) promise2.then((res) =&gt; { console.log(res) Promise.resolve().then(() =&gt; { console.log('promise3') }) }) console.log('script end') //1.'script start' //2.'a1 start' //3.'a2' //4.'promise2' //5.'script end' //6.'promise1' //7.'a1 end' //8.'promise2.then' //9.'promise3' //10.'setTimeout'</code></pre> <h4></h4>

页面列表

ITEM_HTML