promiseHndle()
function promiseHndle(){
console.log('main-1')
function foo(param){
var p = new Promise(function(resolve, reject) {
console.log('promise-1')
if (param === 1) {
resolve('1')
} else {
reject('2')
}
console.log('promise-2')
})
return p
}
console.log('main-2')
// promise 中返回的结果为resolve,reject两个状态,
// 如果返回rosole(),那之后进行then的第一个函数
// 如果返回是reject()函数,那就会进入then的第二个函数,如果第二个参数不存在,如果存 在catch函数的话就进入catch函数中执行
//如果返回是reject()函数,那就会进入then的第二个函数,如果第二个参数不存在,后面一直有then函数,就会走then函数的第二个函数,
// 如果最后不存在第二个函数和不存在catch函数的,会抛出一个错误
// 进入catch之后,状态reject变成了resolve;如果后面继续跟着then,则会进入then的第一个函数
foo(2).then(()=>{
console.log('正确的1')
}).then(()=>{
console.log('正确的2')
}).then(()=>{
console.log('正确的3')
},()=>{
console.log('错误的3');
throw new Error('错误的3---')
}).catch(()=>{
console.log('捕获错误')
})
console.log('main-3')
}
可以进行代码自己多练习;争取说的更清楚