promise 练习题(三)

2020-06-06  本文已影响0人  老衲不生气

Promise中的then、catch、finally

需要理解的知识点:

//1、
const promise = new Promise((resolve, reject) => {
  resolve("success1");
  reject("error");
  resolve("success2");
});
promise
.then(res => {
    console.log("then: ", res);
  }).catch(err => {
    console.log("catch: ", err);
  })
提示
执行结果

"then: success1"

————————————————————————————————————————

//2、
const promise = new Promise((resolve, reject) => {
  reject("error");
  resolve("success2");
});
promise
.then(res => {
    console.log("then1: ", res);
  }).then(res => {
    console.log("then2: ", res);
  }).catch(err => {
    console.log("catch: ", err);
  }).then(res => {
    console.log("then3: ", res);
  })
提示
执行结果

"catch: " "error"
"then3: " undefined

————————————————————————————————————————

//3、
Promise.resolve(1)
  .then(res => {
    console.log(res);
    return 2;
  })
  .catch(err => {
    return 3;
  })
  .then(res => {
    console.log(res);
  });
分析
执行结果

1
2

————————————————————————————————————————

//4、
Promise.reject(1)
  .then(res => {
    console.log(res);
    return 2;
  })
  .catch(err => {
    console.log(err);
    return 3
  })
  .then(res => {
    console.log(res);
  });
执行结果

1
3

————————————————————————————————————————

//5、
const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    console.log('timer')
    resolve('success')
  }, 1000)
})
const start = Date.now();
promise.then(res => {
  console.log(res, Date.now() - start)
})
promise.then(res => {
  console.log(res, Date.now() - start)
})
分析
执行结果

'timer'
success 1001
success 1002

————————————————————————————————————————

//6、
Promise.resolve().then(() => {
  return new Error('error!!!')
}).then(res => {
  console.log("then: ", res)
}).catch(err => {
  console.log("catch: ", err)
})
分析
执行结果

"then: " "Error: error!!!"

————————————————————————————————————————

//7、
const promise = Promise.resolve().then(() => {
  return promise;
})
promise.catch(console.err)
分析过程
执行结果

Uncaught (in promise) TypeError: Chaining cycle detected for promise #<Promise>

————————————————————————————————————————

//8、
Promise.resolve(1)
  .then(2)
  .then(Promise.resolve(3))
  .then(console.log)
分析
执行结果

1

————————————————————————————————————————

//9、
Promise.reject('err!!!')
  .then((res) => {
    console.log('success', res)
  }, (err) => {
    console.log('error', err)
  }).catch(err => {
    console.log('catch', err)
  })
分析过程
执行结果

'error' 'error!!!'

————————————————————————————————————————

//10、
Promise.resolve()
  .then(function success (res) {
    throw new Error('error!!!')
  }, function fail1 (err) {
    console.log('fail1', err)
  }).catch(function fail2 (err) {
    console.log('fail2', err)
  })
分析过程
执行结果

fail2 Error: error!!! at success

————————————————————————————————————————

//11、
Promise.resolve('1')
  .then(res => {
    console.log(res)
  })
  .finally(() => {
    console.log('finally')
  })
Promise.resolve('2')
  .finally(() => {
    console.log('finally2')
    return '我是finally2返回的值'
  })
  .then(res => {
    console.log('finally2后面的then函数', res)
  })
分析过程
执行结果

'1'
'finally2'
'finally'
'finally2后面的then函数' '2'

上一篇 下一篇

猜你喜欢

热点阅读