Created
June 29, 2021 05:02
-
-
Save devsnek/772d5b831a84eae7a886ef7dc63e3048 to your computer and use it in GitHub Desktop.
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
import { setTimeout } from 'timers/promises'; | |
import http from 'http'; | |
async function expensiveFunction() { | |
await setTimeout(1000); | |
return 'Here is the important data!'; | |
} | |
http.createServer((req, res) => { | |
if (req.url !== '/') { | |
res.writeHead(404); | |
res.end(); | |
return; | |
} | |
res.writeHead(200, { | |
'Content-Type': 'text/html; charset=utf-8', | |
}); | |
res.write(`\ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Demo</title> | |
</head> | |
<body> | |
<h1>Very Cool Demo</h1> | |
<div id="suspense">Loading Data...</div> | |
</body> | |
</html> | |
`); | |
expensiveFunction() | |
.then((result) => { | |
res.end(`\ | |
<div hidden id="lazy"> | |
${result} | |
</div> | |
<script> | |
lazy.removeAttribute('hidden'); | |
suspense.replaceChildren(lazy); | |
</script> | |
`); | |
}); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you missed
const lazy = document.getElementById('lazy')
andconst suspense = document.getElementById('suspense')