【问题标题】:Promise object with the use of fetch in javascript在 javascript 中使用 fetch 的 Promise 对象
【发布时间】:2020-07-16 18:57:42
【问题描述】:

检查这个例子:

fetch('/article/promise-chaining/user.json')
  .then(response => response.json())
  .then(user => fetch(`https://api.github.com/users/${user.name}`))
  .then(response => response.json())
  .then(githubUser => new Promise(function(resolve, reject) { // (*)
    let img = document.createElement('img');
    img.src = githubUser.avatar_url;
    img.className = "promise-avatar-example";
    document.body.append(img);

    setTimeout(() => {
      img.remove();
      resolve(githubUser); // (**)
    }, 3000);
  }))
  // triggers after 3 seconds
  .then(githubUser => alert(`Finished showing ${githubUser.name}`));

它正在工作。但我的问题是为什么不先执行 image.remove(),图像在 alert(Finished showing ${githubUser.name}) 后被删除。

这里是示例链接:https://javascript.info/promise-chaining

【问题讨论】:

    标签: promise fetch


    【解决方案1】:

    img.remove() 是一个异步操作,它会在下一次渲染时有效显示,所以在你提醒之后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2021-09-20
      • 2022-11-14
      相关资源
      最近更新 更多