1. 嵌套使用方式是食用promise最常见的误区
      1. function ajax (url) {
      2. return new Promise(function (resolve, reject) {
      3. let xhr = new XMLHttpRequest()
      4. xhr.open('GET', url)
      5. xhr.responseType = 'json'
      6. xhr.onload = function () {
      7. if (this.status + '' === '200') {
      8. resolve(this.response)
      9. console.log(this.response)
      10. } else {
      11. reject(new Error(this.responseText))
      12. console.log(this.responseText)
      13. }
      14. }
      15. xhr.send()
      16. })
      17. }
      18. ajax('./ajaxTest.json')
      19. // promise嵌套使用误区, 这种嵌套使用其实就是借助promise.then的链式调用的特点, 但是没有保证异步任务的扁平化
      20. ajax(url).then(res => {
      21. ajax(res).then(user => {
      22. ajax(user.name).then(result => {
      23. console.log(result)
      24. })
      25. })
      26. })