Skip to content Skip to sidebar Skip to footer

How Can I Make "onload" Waiting For "fetch" Complete?

Long story short, I'll show the code. some.html some.js window.onload = () => console.log( 'onload' ); (asy

Solution 1:

What I think you are trying to achieve is the fetch starts before window.onload, but onload needs to wait for the fetch before doing anything else ...

const promiseOfSomeData = fetch("some.json").then(r=>r.json()).then(data => {
    console.log('in async');
    return data;
});
window.onload = async () => {
    let someData = await promiseOfSomeJsonData;
    console.log("onload");
};

Post a Comment for "How Can I Make "onload" Waiting For "fetch" Complete?"