From 75efbc27201b227507567e46c33f5d0ca944510d Mon Sep 17 00:00:00 2001 From: Justin Poehnelt Date: Mon, 8 Jul 2024 15:04:38 -0600 Subject: [PATCH] post: modify webassembly code --- src/posts/apps-script-async-await.md | 36 +++++++++++++--------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/posts/apps-script-async-await.md b/src/posts/apps-script-async-await.md index 171cb96c..7ea9f4bf 100644 --- a/src/posts/apps-script-async-await.md +++ b/src/posts/apps-script-async-await.md @@ -66,7 +66,7 @@ async function main() { } ``` -This leads to an interesting discovery, the entry function can be `async` and the `await` keyword can be used. But can I use a top level await? +This leads to an interesting discovery, the entry function can be `async` and the `await` keyword can be used. But can I use a top-level await? ```javascript const promise = new Promise((resolve, reject) => { @@ -80,7 +80,7 @@ function main() { } ``` -No, top level await is not supported in Apps Script and returns the following error when trying to save the script: +No, top-level await is not supported in Apps Script and returns the following error when trying to save the script: ```sh Syntax error: SyntaxError: await is only valid @@ -94,10 +94,10 @@ There is no `unhandledRejection` error for promises in Apps Script. Combined wit ## WebAssembly in Apps Script -As mentioned earlier, the only place I've seen async and await actually useful in Apps Script is with the WebAssembly API. Here is a simple example of using async and await with WebAssembly: +As mentioned earlier, the only place I've seen `async` and `await` useful in Apps Script is with the WebAssembly API. Here is a simple example of using async and await with WebAssembly: ```javascript -async function main() { + async function main() { let bytes = new Uint8Array( Utilities.base64Decode( 'AGFzbQEAAAABBwFgAn9/AX8DAgEAB' + @@ -107,37 +107,33 @@ async function main() { ) ); + const promise = WebAssembly.instantiate(bytes); + promise.then(() => console.log("instantiated")); + console.log("next"); + let { instance: { exports: { add } } - } = await WebAssembly.instantiate(bytes); + } = await promise; - console.log(add(1, 2)); + console.log("add(1,2)", add(1, 2)); } ``` This output works as expected: ```sh -11:44:38 AM Notice Execution started -11:44:38 AM Info 3 -11:44:38 AM Notice Execution completed -``` - -However, to verify that the code is actually running asynchronously, I removed the `await WebAssembly.instantiate` which results in: - -```javascript -11:45:57 AM Notice Execution started -11:45:58 AM Error -TypeError: Cannot read properties of - undefined (reading 'exports') - main @ Code.gs:6 +3:00:58 PM Notice Execution started +3:00:58 PM Info next +3:00:58 PM Info instantiated +3:00:58 PM Info add(1,2) 3 +3:00:59 PM Notice Execution completed ``` -So, it is clear that the WebAssembly API is actually running asynchronously and populating the `instance.exports` object after the `instantiate` method is called. +So, it is clear that the WebAssembly API is running asynchronously and not blocking the task queue. ## Conclusion