Skip to content

Instantly share code, notes, and snippets.

Created March 20, 2022 19:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A simple demo of the top-level await keyword in vanilla JS.
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<p>Check the JavaScript console.</p>
<script src="index.js" type="module"></script>
import users from "./users.js";
if (users instanceof Error) {
} else {
const API = "";
let users;
async function getJSON(response) {
if (response.ok) {
const data = await response.json();
return Promise.resolve(data);
const { status, statusText } = response;
const error = new Error(`${status} ${statusText}`);
return Promise.reject(error);
try {
const response = await fetch(API);
users = await getJSON(response);
} catch (error) {
users = error;
export default users;
Copy link

JavaScript modules won’t run if you try to open an HTML file in the browser using the file:// origin. You need to run a local web server on your computer.

Here's a list of one-liners for starting up a web server.

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