可以等待一个不返回任何内容的函数。

huangapple go评论75阅读模式
英文:

Is it possible to await a function that doesn't return anything

问题

我有一个for循环,它从另一个文件中获取代码,并使用insertAdjacentHTML()方法将其附加到我的网站。之后,它会修改新添加的代码片段的innerHTML。我的当前解决方案看起来像这样:

for (let i = 0; i < 3; i++) {
    fetch("myFile.html")
    .then((obj) => obj.text())
    .then((text) => {
        document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
        document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
    });
}

在我的网站上,我有以下代码:

<div id="addContentHere"></div>

我要获取的HTML大致如下:

<div class="addedElement">
    <div class="elementInAddedContent"></div>
</div>

因此,我的网站最终结果应该如下所示:

<div id="addContentHere">
    <div class="addedElement">
        <div class="elementInAddedContent">Modified content</div>
    </div>
    <div class="addedElement">
        <div class="elementInAddedContent">Modified content</div>
    </div>
    <div class="addedElement">
        <div class="elementInAddedContent">Modified content</div>
    </div>
</div>

当然,所有添加的元素都将具有不同的内容,但出于示例目的,我保持了简单。问题是计算机在元素添加之前就到达了document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";这一行,因此我会收到Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'innerHTML')错误。我是否可以在不返回任何内容的代码上使用await?就像这样:

for (let i = 0; i < 3; i++) {
    fetch("myFile.html")
    .then((obj) => obj.text())
    .then(async (text) => {
        await document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
        document.getElementsByClassName("elementInAddedContent")[i].innerHTML = "Modified content";
    });
}
英文:

I have a for loop that fetches code from another file and appends it to my website using the insertAdjacentHTML() method. After that it modifies the innerHTML of the freshly added piece of code. My current solution looks something like this:

for (let i = 0; i &lt; 3; i++) {
    fetch(&quot;myFile.html&quot;)
    .then((obj) =&gt; obj.text())
    .then((text) =&gt; {
        document.getElementById(&quot;addContentHere&quot;).insertAdjacentHTML(&quot;beforeend&quot;, text);
        document.getElementsByClassName(&quot;elementInAddedContent&quot;)[i].innerHTML = &quot;Modified content&quot;;
    });
}

In my website I have the following code:

&lt;div id=&quot;addContentHere&quot;&gt;&lt;/div&gt;

And the HTML that I'm fetching is something like this:

&lt;div class=&quot;addedElement&quot;&gt;
    &lt;div class=&quot;elementInAddedContent&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

So the end result in my website should look like this:

&lt;div id=&quot;addContentHere&quot;&gt;
    &lt;div class=&quot;addedElement&quot;&gt;
        &lt;div class=&quot;elementInAddedContent&quot;&gt;Modified content&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;addedElement&quot;&gt;
        &lt;div class=&quot;elementInAddedContent&quot;&gt;Modified content&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;addedElement&quot;&gt;
        &lt;div class=&quot;elementInAddedContent&quot;&gt;Modified content&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

Of course all added elements are going to have different content, but for example purpouses I'm keeping it simple. The problem is that the computer reaches the document.getElementsByClassName(&quot;elementInAddedContent&quot;)[i].innerHTML = &quot;Modified content&quot;; before the element has been added and so I get the Uncaught (in promise) TypeError: Cannot set properties of undefined (setting &#39;innerHTML&#39;) error. Can I somehow use await on code that doesn't itself return anything? So something like this:

for (let i = 0; i &lt; 3; i++) {
    fetch(&quot;myFile.html&quot;)
    .then((obj) =&gt; obj.text())
    .then((text) =&gt; {
        await document.getElementById(&quot;addContentHere&quot;).insertAdjacentHTML(&quot;beforeend&quot;, text);
        document.getElementsByClassName(&quot;elementInAddedContent&quot;)[i].innerHTML = &quot;Modified content&quot;;
    });
}

答案1

得分: 1

Your loop is trying as fast as possible to read a file, update the DOM and read again and update again. There is no control over what is happening since Fetch is async. I still do not understand why you had a loop around getting ONE file..

Perhaps you meant to do this?

I give the DOM 100 ms to be updated

fetch("myFile.html")
  .then((obj) => obj.text())
  .then((text) => {
    document.getElementById("addContentHere").insertAdjacentHTML("beforeend", text);
    setTimeout(() => document.querySelectorAll(".elementInAddedContent").forEach(el => el.textContent = "Modified content"), 100);
  });
英文:

Your loop is trying as fast as possible to read a file, update the DOM and read again and update again. There is no control over what is happening since Fetch is async. I still do not understand why you had a loop around getting ONE file..

Perhaps you meant to do this?

I give the DOM 100 ms to be updated

<!-- language: lang-js -->

fetch(&quot;myFile.html&quot;)
  .then((obj) =&gt; obj.text())
  .then((text) =&gt; {
    document.getElementById(&quot;addContentHere&quot;).insertAdjacentHTML(&quot;beforeend&quot;, text);
    setTimeout(() =&gt; document.querySelectorAll(&quot;.elementInAddedContent&quot;).forEach(el =&gt; el.textContent = &quot;Modified content&quot;), 100);
  });

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 16:08:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定