es6中的promise相关

// 例子1
  const testPromise = new Promise(function (resolve, reject) {
    let a = 5
    if (a === 5) {
      resolve('success')
    } else {
      reject('catch error2')
    }
  })
  // resolve 结果没法处理,一直抛出去
  testPromise.then(null, (res) => {
    console.log(res, 1)
  }).then(null, res => {
    console.log(res, 2)
  }).then(res => {
    console.log(res, 3)
  })

// 例子2
const testPromise = new Promise(function (resolve, reject) {
    let a = 5
    if (a === 5) {
      resolve('success')
    } else {
      reject('catch error2')
    }
  })
  testPromise.then(res => { console.log('res1', res); return 123}, err => { console.log('err1', err)})
    .then(res => { console.log('res2', res)}, err => { console.log('err2', err); return Promise.reject(456);})
    .then(res => { console.log('res3', res)}, err => { console.log('err3', err)})
    .then(res => { console.log('res4', res)}, err => { console.log('err4', err)})

87BCF1C372C9D7FF8013EB6EC00D8517.png

AF1C4ADC74288ED1B08554A85194FF08.png

标签: none