Skip to content

[p5.js 2.0 Beta Bug Report]: await not working for async load functions #7587

Closed
@sidwellr

Description

@sidwellr

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.0.0-beta.3

Web browser and version

Brave 1.75.178, Chrome 133.0.6943.142, Firefox 135.0

Operating system

Windows 10

Steps to reproduce this

Many of the p5.js load functions don't work correctly with await; they seem to return prematurely. I use loadStrings in the snippet below, but have seen the same problem in loadShader and loadBytes; it is probably an issue with others. In the code below, I use "await loadStrings" to load data from a text file, which should wait until the data is loaded to continue. But printing it immediately prints an empty array. The successCallback works fine; the correct data is printed in the callback function. (But the problem arises even if no callback function is given.)

Steps:

  1. Call await loadStrings (or loadShader or loadBytes) in the async setup function
  2. The await should make the function wait until the data is available, but the data is empty

Snippet:

https://editor.p5js.org/rsidwell/sketches/aVQd6eoOG

let myData;

async function setup() {
  myData = await loadStrings('test.txt', gotData);  
  print(myData);
}

function gotData(d) {
  myData = d;
  print(myData);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions