title

Promise 当做状态机使用


const fn = (num) =>{ if(num > 2){ return '数字大于2' } if(num === undefined){ return '数字不存在' } return num }
  1. 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))
  1. Promise Async 状态机 改写:
const fn = async(num) =>{ if(num > 2){ return Promise.reject('数字大于2') } if(num === undefined){ return Promise.reject('数字不存在') } return num }
  1. Promise Async Throw 状态机 改写:
const fn = async(num) =>{ if(num > 2){ throw '数字大于2' } if(num === undefined){ throw '数字不存在' } return num }