Created
March 2, 2020 09:18
-
-
Save Goloburda/b34bda997d14d7e942d1ec24c529e71d to your computer and use it in GitHub Desktop.
React Suspense?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Author
Goloburda
commented
Mar 2, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment