Skip to content

Async callbacks? #31

@danielmahal

Description

@danielmahal

Thanks for this library, neat for prototyping!

Is there a way to use async callbacks?

This is my case:

import { Webview } from "webview-bun";

const html = await Bun.file("index.html").text();

const webview = new Webview();

webview.bind("setName", async (name) => {
  console.log("Setting name to", name);
  await Bun.write("name.txt", name);
  console.log('File written "name.txt"', name);
});

const initialName = await Bun.file("name.txt").text();

webview.bind("initialName", () => {
  return initialName;
});

webview.title = "Bun App";

webview.setHTML(`
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="input" />
    <script type="module">
      input.value = await initialName();

      input.addEventListener("input", (e) => {
        setName(e.target.value).then((res) => {
          console.log(res);
        });
      });
    </script>
  </body>
</html>
`);

webview.run();

Steps to reproduce:

  1. Run the app above
  2. Type something in the input field

Expected behavior:

  • Logs out "Setting name to…"
  • Writes file
  • Logs out "File written…"

Actual behavior:

  • Logs out "Setting name to…"
  • Writes file
  • Logs out "File written…"

Seems like it stalls at/after the await in setName. The following code isn't executed.

This is a simplified example by the way, I discovered this when I wanted to perform a check if the file exists (await Bun.file('name').exists()), before performing other actions (writing, etc…)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions