Skip to content

Instantly share code, notes, and snippets.

@gonzaloruizdevilla
Created September 5, 2019 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonzaloruizdevilla/e85a6395b1432ef2385d9aaf532c4fb4 to your computer and use it in GitHub Desktop.
Save gonzaloruizdevilla/e85a6395b1432ef2385d9aaf532c4fb4 to your computer and use it in GitHub Desktop.
async function maybeInstantiateStreaming(path, ...opts) {
// Start the download asap.
const f = fetch(path);
try {
// This will throw either if `instantiateStreaming` is
// undefined or the `Content-Type` header is wrong.
return WebAssembly.instantiateStreaming(
f,
...opts
);
} catch(_e) {
// If it fails for any reason, fall back to downloading
// the entire module as an ArrayBuffer.
return WebAssembly.instantiate(
await f.then(f => f.arrayBuffer()),
...opts
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment