Skip to content

Support for callbacks in async functions #683

@almarklein

Description

@almarklein

For the async API functions in wgpu, we currently offer two options: busy-wait for it, or async-wait for it.

In JS, an async function returns a promise, and this offers a 3d option: to provide a callback that is called when the promise resolves. I had completely overlooked this option. It's interesting, because its an option that works in sync (not-async) code, and can be made to work the same way in Python and JS/Pyodide.

I probably need to think about this more, also how this plays with rendercanvas and pygfx, but what I currently think would be a good plan is to have all the async API functions return a Promise-like object, which can then be used to:

  • promise.busy_wait() - Python only, for code that does not care waiting a bit
  • await promise - works in Python and JS
  • promise.then(callback) - Python and JS

(we already have an object that does the first two things, but it's used internally (not public) and called WgpuAwaitable)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions