如何在承诺中检查/调试个别承诺的状态。

发布于 2025-02-08 08:34:32 字数 1108 浏览 0 评论 0原文

我将一堆脚本节点推入没有src属性中的一系列承诺,这些脚本节点设置了src ,然后resolve> resolve s或拒绝 s,分别onloadonerror。所有这些均以promise.ly等待的方式调用。问题是,它永远不会停止等待。我认为一些个人承诺正在解决或拒绝,但是我不知道如何调试哪些不是。

  function loadScript (script) {
    return new Promise(function(resolve, reject) {
      // this happens for every script
      script.setAttribute("src", script.getAttribute("data-src"));
      script.onload = _ => resolve(script.src);
      script.onerror = _ => reject(script.src);
    });
  }
  
  // loadScripts : (graph, int) -> (string array) promise
  async function loadScripts(g, order = 1) {
    if (!g.has(order))
      return []
    else
      return [
        ...await Promise.all(g.get(order).map(loadScript)),
        // the script stops here after the first pass and never executes the
        // next line. It just ... awaits the previous line
        ...await loadScripts(g, order + 1)
      ]
  }

我通过在Chrome Devtools和Steppping中设置断点来实现这一点,但是我不确定,一旦我们坐着等待,如何检查个人承诺的状态,看看哪些正在解决,哪些仍在。

I'm shoving a bunch of script nodes with no src attribute into an array of promises that sets the src and then resolves or rejects, respectively onload or onerror. All of these are called in a Promise.all which I await. The problem is, it never stops waiting. I presume some of the individual promises are resolving or rejecting, but I can't figure out how to debug which ones aren't.

  function loadScript (script) {
    return new Promise(function(resolve, reject) {
      // this happens for every script
      script.setAttribute("src", script.getAttribute("data-src"));
      script.onload = _ => resolve(script.src);
      script.onerror = _ => reject(script.src);
    });
  }
  
  // loadScripts : (graph, int) -> (string array) promise
  async function loadScripts(g, order = 1) {
    if (!g.has(order))
      return []
    else
      return [
        ...await Promise.all(g.get(order).map(loadScript)),
        // the script stops here after the first pass and never executes the
        // next line. It just ... awaits the previous line
        ...await loadScripts(g, order + 1)
      ]
  }

I got to this point by setting breakpoints in Chrome devtools and stepping, but I'm not sure, once we're sitting and awaiting, how to check the state of the individual promises, to see which ones are resolving and which are still pending.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文