ForeveHG / Frontend-Daily-Interview

学习,尝试回答一些前端面试题
1 stars 0 forks source link

12. Promise运行结果 #12

Open ForeveHG opened 5 years ago

ForeveHG commented 5 years ago

运行环境Mac chrome 78.0.3904.87

ForeveHG commented 5 years ago
new Promise((resolve, reject) => {
    resolve(); 
}).then(() => {
    console.log("promise1 resolved");
    return Promise.resolve(2)
}).then((r) => {
    console.log('----',r)
},error => {
    console.log('error', error)
})
new Promise((resolve, reject) => {
    resolve(); 
}).then(() => { 
    console.log("promise2 resolved");
}).then(() => {
    console.log("promise3 resolved")
}).then(() => {
    console.log("promise4 resolved")
}).then(() => {
    console.log("promise5 resolved")
})
运行结果
promise1 resolved
promise2 resolved
promise3 resolved
promise4 resolved
---- 2
promise5 resolved
ForeveHG commented 5 years ago
new Promise(resolve => {
    console.log(1);
    Promise.resolve().then(()=> console.log(4))
    resolve(3);
}).then(num => {
    console.log(num)
});
console.log(2)
ForeveHG commented 5 years ago
var p3 = new Promise(function(resolve, reject) {
    resolve('B')
})

var p1 = new Promise(function(resolve,reject) {
    resolve(p3)
})

var p2 = new Promise(function(resolve, reject){
    resolve("A")
})

p1.then(function(v) {
    console.log(v)
})

p2.then(function(v){
    console.log(v)
})
ForeveHG commented 5 years ago
var p1 = new Promise(function(resolve,reject) {
    resolve('A')
})

p1.then(function(v) {
    console.log(vv)
},function(vv){
    console.log(1, vv)
})

结果:Uncaught (in promise) ReferenceError: vv is not defined 说明: then中注册的两个回调函数是p1的resolve,和p1的reject,而p1在new Promise的函数中已经决议为resolve状态,已经决议的状态不会再改变,所以不可能再调用p1的reject函数。 p1.then会返回另一个promise,这个promise会因为这个异常被reject,但代码中没有提供这个promise的reject函数,所以会报这个错误,修改一下这个代码

var p1 = new Promise(function(resolve,reject) {
    resolve('A')
})

p1.then(function(v) {
    console.log(vv)
},function(vv){
    console.log(1, vv)
}).catch(function(error){
    console.log(error)
})
//或者
var p1 = new Promise(function(resolve,reject) {
    resolve('A')
})

p1.then(function(v) {
    console.log(vv)
},function(vv){
    console.log(1, vv)
}).then(null, function(error){
    console.log(error)
})
ForeveHG commented 5 years ago
async function fn() {
    await Promise.reject('出错了')
    console.log('--')
}
fn().then(v => console.log('success',v)).catch(e => console.log('error',e))
new Promise((resolve) => {
    console.log(1)
    resolve()
}).then(() => {
    console.log(2)
}).then(() => {
    console.log(3)
}).then(() => {
    console.log(4)
})
结果
1
2
3
error 出错了
4
ForeveHG commented 4 years ago
async function fn() {
    await Promise.reject('出错了')
    console.log('--')
}
fn().then(v => console.log('success', v), e => console.log('error', e))
new Promise((resolve) => {
    console.log(1)
    resolve()
}).then(() => {
    console.log(2)
}).then(() => {
    console.log(3)
}).then(() => {
    console.log(4)
}).then(() => {
    console.log(5)
}).then(() => {
    console.log(6)
})
浏览器结果(运行环境Mac chrome 78.0.3904.87)
1
2
error 出错了
3
4
5
6
node结果(node v10.16.0)
1
2
3
4
error 出错了
5
6