MZ Javascript help with scriptable events involving an external JSON file

Hello all,

I am really new to RPG Maker MZ and was wondering if you guys have any great resources on javascript for script events involving external resource grabbing. This is not for plugin creation, but for adding more functionality in a particular event. This event will draw data using an asynchronous fetch call. The code I have created so far looks like so:

let msg = "not apple"; async function getJSONData() { try { $gameMessage.newPage(); $gameMessage.setFaceImage('Actor1', 0); $gameMessage.add('Ok, I am in the function...'); const response = await fetch('https://dummyjson.com/products'); const data = await response.json(); //return data; // Do something with the JSON data msg = data["products"][0].brand; player.setMoveSpeed(4); $gameMessage.newPage(); $gameMessage.setFaceImage('Actor1', 0); $gameMessage.add('The fetch command says ' + msg + " but I don't know what that even means"); } catch (error) { console.error('Error:', error); } } $gameMessage.setFaceImage('Actor1', 0); $gameMessage.add('Trying to grab from database...'); player = $gamePlayer; player.setMoveSpeed(0); getJSONData();

Currently, the code works when I test the event, but in the play tests, the game will skip over the display message part (Console output shows that the file is fetched properly). Is there a better way to approach this? Thank you!!
Moved to: Learning Javascript

I'm not sure how you added your code, but the code tags aren't right and your script isn't showing correctly.
Moved to: Learning Javascript

I'm not sure how you added your code, but the code tags aren't right and your script isn't showing correctly.
JavaScript:
let msg = "not apple";
async function getJSONData() {
  try {
    $gameMessage.newPage();
    $gameMessage.setFaceImage('Actor1', 0);
    $gameMessage.add('Ok, I am in the function...');

    const response = await fetch('https://dummyjson.com/products');
    const data = await response.json();
    //return data;
    // Do something with the JSON data
    msg = data["products"][0].brand;
    player.setMoveSpeed(4);
    $gameMessage.newPage();
    $gameMessage.setFaceImage('Actor1', 0);
    $gameMessage.add('The fetch command says ' + msg + " but I don't know what that even means");;
console.log("made it through the function");
console.log(data);
  } catch (error) {
    console.error('Error:', error);
  }
}

$gameMessage.setFaceImage('Actor1', 0);
    $gameMessage.add('Trying to grab from database...');
player = $gamePlayer;
player.setMoveSpeed(0);
getJSONData();
console.log("end of line");

Is this better? And thank you for moving it to a more relevant forum!

Original thread: https://forums.rpgmakerweb.com/threads/javascript-help-with-scriptable-events-involving-an-external-json-file.156964/