可取消的 Promise

https://medium.com/@benlesh/promise-cancellation-is-dead-long-live-promise-cancellation-c6601f1f5082

proposal-cancelable-promises 被撤销了

  • 可以使用 Bluebird 的 Promise ```javascript var result = makeCancellableRequest(…);

var firstConsumer = result.then(…); var secondConsumer = result.then(…);

  1. 考虑 promise 有多个消费者等情况<br />详见文档 [http://bluebirdjs.com/docs/api/cancellation.html](http://bluebirdjs.com/docs/api/cancellation.html)
  2. Promise.race( ) 被丢弃的那个 promise 还是会执行完
  3. ```javascript
  4. let p1 = new Promise(r => {
  5. setTimeout(() => {
  6. console.log('p1 start');
  7. r('p1')
  8. }, 2000);
  9. })
  10. let p2 = new Promise(r => {
  11. setTimeout(() => {
  12. console.log('p2 start');
  13. r('p2')
  14. }, 200);
  15. })
  16. Promise.race([p1, p2]).then(who => console.log(who + ' done'))
  17. // p2 start
  18. // p2 done
  19. // p1 start

取消请求

https://github.com/axios/axios#cancellation

参考文档