Skip to content

Commit

Permalink
post: modify webassembly code
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt committed Jul 8, 2024
1 parent 551f68e commit 75efbc2
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/posts/apps-script-async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand All @@ -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' +
Expand All @@ -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:57AM Notice Execution started
11:45:58AM 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

Expand Down

0 comments on commit 75efbc2

Please sign in to comment.