Skip to content

Instantly share code, notes, and snippets.

@xydiva
Created May 31, 2018 14:27
Show Gist options
  • Save xydiva/548a9233400460b32174c5432bf23f9a to your computer and use it in GitHub Desktop.
Save xydiva/548a9233400460b32174c5432bf23f9a to your computer and use it in GitHub Desktop.
promise测试题
console.log(1);
new Promise(function (resolve, reject){
    reject(true);
    window.setTimeout(function (){
        resolve(false);
    }, 0);
}).then(function(){
    console.log(2);
}, function(){
    console.log(3);
});
console.log(4);

// 143

promise内部实现是异步的,所以14先输出
reject函数调用输出3也没有问题
关键是这个setTimeout,理论部分有特意讲到,promise的状态一旦改变是无法再修改的,所以resolve根本就不会执行,因此也不会输出2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment