本项目为饥人谷系统班大厂冲刺课程,不接受转载。如需了解合作咨询vx: xiedaimala03
以下内容视频讲解在这里
Promise 相关问题
代码输出题
setTimeout(() => console.log(1), 0);
new Promise(resolve => {
resolve();
console.log(2);
}).then(() => {
console.log(3);
});
console.log(4);
new Promise(function f1(resolve){
console.log(1);
setTimeout(function f2() {
console.log(2);
});
resolve(1);
}).then(function f3(res) {
console.log(3);
})
setTimeout(function f4() {
console.log(4);
})
console.log(5);
setTimeout(function f1() {
console.log(1)
})
new Promise(function f2(resolve) {
resolve()
console.log(2)
}).then(function f3() {
console.log(3)
Promise.resolve().then(function f4() {
console.log(4)
}).then(function f5() {
Promise.resolve().then(function f6() {
console.log(5)
})
})
})
console.log(6)
console.log(1)
setTimeout(function f1(){
console.log(2)
Promise.resolve().then(function f2() {
console.log(3)
setTimeout(function f3() {
console.log(4)
})
}).then(function f4() {
console.log(5)
})
}, 0)
console.log(6)
const pro = new Promise(function f1(resolve) {
const innerpro = new Promise(function f2(resolve) {
setTimeout(function f3(){
resolve(1);
}, 0);
console.log(2);
resolve(3);
});
innerpro.then(function f4(res) {console.log(res)} );
resolve(4);
console.log(5);
})
pro.then(function f5(res) { console.log(res) });
console.log(6);
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2 start');
return new Promise((resolve, reject) => {
resolve();
console.log('async2 promise');
})
}
console.log('script start');
setTimeout(function() {
console.log('setTimeout');
}, 0);
async1();
new Promise(function(resolve) {
console.log('promise1');
resolve();
}).then(function() {
console.log('promise2');
}).then(function() {
console.log('promise3');
});
console.log('script end');
new Promise((resolve, reject) => {
reject('error')
}).then(()=> {
console.log('ok 1')
}, (err) => {
console.log('error 1: ' + err)
}).then(() => {
console.log('ok 2')
}, (err) => {
console.log('error 2: ' + err)
}).catch(err => {
console.log('catch 1: ' + err)
})