This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
javascript synchoner fetch
#1
Ich möchte einen synchronen json-fetch-Aufruf ausführen. Siehe den folgenden Code.
Es funktioniert im Prinzip, aber nicht in der erwarteten Reihenfolge (nicht synchron, sondern wieder asynchron).
Meine Log-Erwartung ist 1 2 3 4 5 6 7, aber ich erhalte 1 2 3 7 4 5 6
Wie erfülle ich meine Erwartung?

console.log("1 start");
async function fetchInfo() {
    let url = `https://reqres.in/api/products/3`; // only for test
    console.log("  3 fetch()");
    let response = await fetch(url);
    console.log("  4 json()");
    let data = await response.json()
    console.log("  5 return");
    return data;
}
console.log("2 call fetchInfo()")
fetchInfo()
    .then(data => console.log("6 json object: ...", data));
console.log("7 end / more code ...");
Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 2 Gast/Gäste