使用AssemblyScript在360浏览器下报错 SyntaxError: Unexpected reserved word
2022/8/24 23:25:12
本文主要是介绍使用AssemblyScript在360浏览器下报错 SyntaxError: Unexpected reserved word,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
背景
最近在一个项目中使用了AssemblyScript,它能将类似于TypeScript的代码编译为WebAssembly,在其他浏览器都能正常使用,然而在360浏览器上却会报错:SyntaxError: Unexpected reserved word。
原因
先来看AssemblyScript生成的release.js代码
index.ts只包含一个简单的add方法
async function instantiate(module, imports = {}) { const { exports } = await WebAssembly.instantiate(module, imports); return exports; } export const { memory, add } = await (async url => instantiate( await (async () => { try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } })(), { } ))(new URL("release.wasm", import.meta.url));
360浏览器报错锁定在这await一行
} = await (async url => instantiate(
查看360浏览器的内核版本,发现是86.0.4240.198,360浏览器已经是最新的版本了。
而支持在模块顶层使用await的chrome最低版本是89
所以出现了这个错误~
让用户换浏览器是一个不现实的想法。
所以,有没有什么解决方法呢?
解决方法
不使用await就行了,用Promise代替await。
修改release.js export部分改为以下代码
export const exports = new Promise((resolve, reject) => { globalThis.WebAssembly.compileStreaming(globalThis.fetch(new URL("release.wasm", import.meta.url))).then(module => { instantiate(module, {}).then(res => { resolve(res); }).catch((err) => { console.error(err); reject(err); }) }) })
引入
<script type="module"> import { exports } from "./release.js"; exports.then((res) => { // add方法 const { add } = res; add(12, 34); }); </script>
完美。
这篇关于使用AssemblyScript在360浏览器下报错 SyntaxError: Unexpected reserved word的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-25Elevate Your Lead Generation Game with Maps Scraper AI
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能