Javascript why import module which get async data doesn’t trigger DOMContentLoaded and load event

Sorry not a native English speaker.

I have a async data which get from other module to import.

when I import, DOMContentLoaded and load event won’t’ be triggered. And when I comment out import, it work

// main.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><head>
<script type="module">
import test from "/js/test.js";
console.log(test); // has data
// can only work when comment out import
window.addEventListener("DOMContentLoaded", () => {
console.log("DOMContentLoaded");
});
// can only work when comment out import
window.addEventListener("load", () => {
console.log("load");
});
// work no matter import module or not
$(document).ready(() => {
console.log("ready");
});
</script>
</head>
</code>
<code><head> <script type="module"> import test from "/js/test.js"; console.log(test); // has data // can only work when comment out import window.addEventListener("DOMContentLoaded", () => { console.log("DOMContentLoaded"); }); // can only work when comment out import window.addEventListener("load", () => { console.log("load"); }); // work no matter import module or not $(document).ready(() => { console.log("ready"); }); </script> </head> </code>
<head>
    <script type="module">
        import test from "/js/test.js";
        console.log(test); // has data

        // can only work when comment out import
        window.addEventListener("DOMContentLoaded", () => {
            console.log("DOMContentLoaded");
        });

        // can only work when comment out import
        window.addEventListener("load", () => {
            console.log("load");
        });

        // work no matter import module or not
        $(document).ready(() => {
            console.log("ready"); 
        });
    </script>
</head>

// test.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let test = await f();
async function f() {
const r = await fetch("url", {
method: "post",
body: JSON.stringify({
"key": "value"
}),
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
}
})
.then((res) => res.json())
.then((result) => {
return result;
})
return r;
}
console.log(test); // has data
export default test
</code>
<code>let test = await f(); async function f() { const r = await fetch("url", { method: "post", body: JSON.stringify({ "key": "value" }), headers: { "Content-Type": "application/json", "Accept": "application/json", } }) .then((res) => res.json()) .then((result) => { return result; }) return r; } console.log(test); // has data export default test </code>
let test = await f();
async function f() {
    const r = await fetch("url", {
        method: "post",
        body: JSON.stringify({
            "key": "value"
        }),
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
        }
    })
    .then((res) => res.json())
    .then((result) => {
        return result;
    })
    return r;
}
console.log(test); // has data
export default test

And there is another example, load event is work!

// main.js (still put in head)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Data } from '/js/global/Data';
console.log(Data); // has data
// not work
window.addEventListener("DOMContentLoaded", () => {
console.log("DOMContentLoaded");
});
// not work
window.addEventListener("DOMContentLoaded", async () => {
console.log("DOMContentLoaded");
});
// it work!
window.addEventListener("load", () => {
console.log("load");
});
// it work!
window.addEventListener("load", async () => {
console.log("load");
});
</code>
<code>import { Data } from '/js/global/Data'; console.log(Data); // has data // not work window.addEventListener("DOMContentLoaded", () => { console.log("DOMContentLoaded"); }); // not work window.addEventListener("DOMContentLoaded", async () => { console.log("DOMContentLoaded"); }); // it work! window.addEventListener("load", () => { console.log("load"); }); // it work! window.addEventListener("load", async () => { console.log("load"); }); </code>
import { Data } from '/js/global/Data';
console.log(Data); // has data
// not work
window.addEventListener("DOMContentLoaded", () => {
    console.log("DOMContentLoaded");
});

// not work
window.addEventListener("DOMContentLoaded", async () => {
    console.log("DOMContentLoaded");
});

// it work!
window.addEventListener("load", () => {
    console.log("load");
});

// it work!
window.addEventListener("load", async () => {
    console.log("load");
});

// js/global/Data

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { storage_setItem, storage_getItem, storage_remove } from '/js/module/localStorage';
let Data;
storage_remove("Data");
Data = await get() ?? await save(getDefault());
Data = Data.datas;
console.log(Data);
async function get() {
return await storage_getItem("Data");
}
async function save(data) {
await storage_setItem("Data", data);
return await get();
}
function getDefault() {
return {
datas: {}
}
}
export {
Data,
get,
save
}
</code>
<code>import { storage_setItem, storage_getItem, storage_remove } from '/js/module/localStorage'; let Data; storage_remove("Data"); Data = await get() ?? await save(getDefault()); Data = Data.datas; console.log(Data); async function get() { return await storage_getItem("Data"); } async function save(data) { await storage_setItem("Data", data); return await get(); } function getDefault() { return { datas: {} } } export { Data, get, save } </code>
import { storage_setItem, storage_getItem, storage_remove } from '/js/module/localStorage';

let Data;
storage_remove("Data");

Data = await get() ?? await save(getDefault());
Data = Data.datas;

console.log(Data);

async function get() {
    return await storage_getItem("Data");
}

async function save(data) {
    await storage_setItem("Data", data);
    return await get();
}

function getDefault() {
    return {
        datas: {}
    }
}

export {
    Data,
    get,
    save
}

// js/module/localStorage

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Preferences } from '@capacitor/preferences';
async function storage_setItem(key, value) {
await Preferences.set({
key: key,
value: typeof value == "object" ? JSON.stringify(value) : value
});
}
async function storage_getItem(key) {
const ret = await Preferences.get({ key: key });
return typeof JSON.parse(ret.value) == "object" ? JSON.parse(ret.value) : ret.value;
}
async function storage_remove(key) {
await Preferences.remove({ key: key })
}
export {
storage_setItem,
storage_getItem,
storage_remove
}
</code>
<code>import { Preferences } from '@capacitor/preferences'; async function storage_setItem(key, value) { await Preferences.set({ key: key, value: typeof value == "object" ? JSON.stringify(value) : value }); } async function storage_getItem(key) { const ret = await Preferences.get({ key: key }); return typeof JSON.parse(ret.value) == "object" ? JSON.parse(ret.value) : ret.value; } async function storage_remove(key) { await Preferences.remove({ key: key }) } export { storage_setItem, storage_getItem, storage_remove } </code>
import { Preferences } from '@capacitor/preferences';

async function storage_setItem(key, value) {
    await Preferences.set({
        key: key,
        value: typeof value == "object" ? JSON.stringify(value) : value
    });
}

async function storage_getItem(key) {
    const ret = await Preferences.get({ key: key });
    return typeof JSON.parse(ret.value) == "object" ? JSON.parse(ret.value) : ret.value;
}

async function storage_remove(key) {
    await Preferences.remove({ key: key })
}

export {
    storage_setItem,
    storage_getItem,
    storage_remove
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật