const cache = new Map(); const setCallback = (element, key) => { if (cache.has(key)) { return cache.get(key); } throw element; }; const asyncElement = new Promise(res => { setTimeout(() => { res("Mikita"); }, 1000); }); const render = () => { const root = document.querySelector("#app"); if (root.firstChild) { root.firstChild.remove(); } const element = document.createElement("div"); let text = "loading..."; try { text = setCallback(asyncElement, "name"); } catch (promise) { promise.then(data => { cache.set("name", data); render() }); } element.textContent = text; root.appendChild(element); }; render();