Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created June 29, 2021 05:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/772d5b831a84eae7a886ef7dc63e3048 to your computer and use it in GitHub Desktop.
Save devsnek/772d5b831a84eae7a886ef7dc63e3048 to your computer and use it in GitHub Desktop.
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);
@raghavgujjar
Copy link

raghavgujjar commented Jun 30, 2021

you missed const lazy = document.getElementById('lazy') and const suspense = document.getElementById('suspense')

@devsnek
Copy link
Author

devsnek commented Jun 30, 2021

@raghavgujjar element ids automatically become properties of window, no need to query them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment