67、Promise 構造函數是同步執行還是異步執行,那么 then 方法呢?


Promise 構造函數是同步執行還是異步執行,那么 then 方法呢?

解題:

promise構造函數是同步執行的,then方法是異步執行的 歡迎大佬們補充!!

直接上代碼

const promise = new Promise((resolve, reject) => { console.log(1) resolve() console.log(2) }) promise.then(() => { console.log(3) }) console.log(4) 

執行結果是:1243

 

擴展
const promise = new Promise((resolve, reject) => { console.log(1); resolve(5); console.log(2); }).then(val => { console.log(val); }); promise.then(() => { console.log(3); }); console.log(4); setTimeout(function() { console.log(6); }); 

執行結果: 124536

Promise new的時候會立即執行里面的代碼 then是微任務 會在本次任務執行完的時候執行 setTimeout是宏任務 會在下次任務執行的時候執行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM