使用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-01巧用 TiCDC Syncpoint 构建银行实时交易和准实时计算一体化架构
- 2024-05-01银行核心背后的落地工程体系丨Oracle - TiDB 数据迁移详解
- 2024-04-26高性能表格工具VTable总体构成-icode9专业技术文章分享
- 2024-04-16软路由代理问题, tg 无法代理问题-icode9专业技术文章分享
- 2024-04-16程序猿用什么锅-icode9专业技术文章分享
- 2024-04-16自建 NAS 的方案-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数, 加上remote_src: yes 配置-icode9专业技术文章分享
- 2024-04-14ansible 检测远程主机的8080端口,如果关闭,则echo 进程已关闭-icode9专业技术文章分享
- 2024-04-14result 成功怎么写-icode9专业技术文章分享