Promise 当做状态机使用
const fn = (num) =>{
if(num > 2){
return '数字大于2'
}
if(num === undefined){
return '数字不存在'
}
return num
}
- Promise 状态机 改写:
const fn = (num) =>{
if(num > 2){
return Promise.reject('数字大于2')
}
if(num === undefined){
return Promise.reject('数字不存在')
}
return Promise.resolve(num)
}
//使用
fn(num).then(res => {message.success(res)}).catch(err => message.error(err))
- Promise Async 状态机 改写:
const fn = async(num) =>{
if(num > 2){
return Promise.reject('数字大于2')
}
if(num === undefined){
return Promise.reject('数字不存在')
}
return num
}
- Promise Async Throw 状态机 改写:
const fn = async(num) =>{
if(num > 2){
throw '数字大于2'
}
if(num === undefined){
throw '数字不存在'
}
return num
}