MZ Trying to load data from URL

Hello I have a plugin (in spoiler) that I'm simply trying to load data from a url in javascript. Not sure if this works at all in RPG maker, but I can load the plugin and get the console log that it's loaded. I can add the plugin command to an event and the rest of the event runs fine till it gets to the plugin then simply nothing, not an error, not a console log or anything.
Spoiler: Spoiler Script
Code:
/*:
 * @target MZ
 * @plugindesc Example plugin to send fetch commands in RPG Maker MZ.
 * @help FetchExample.js
 *
 * This plugin demonstrates how to use the fetch API to make HTTP requests.
 *
 * @command fetchData
 * @text Fetch Data
 * @desc Fetch data from a specified URL.
 *
 * @arg url
 * @text URL
 * @desc The URL to fetch data from.
 * @type string
 * @default https://jsonplaceholder.typicode.com/todos/1
 */

(() => {
    'use strict';
    const pluginName = 'FetchExample';
    console.log('FetchExample plugin loaded');
    PluginManager.registerCommand(pluginName, 'fetchData', args => {
        const url = args.url;
       
        console.log('FetchExample plugin loaded', url);
        fetch(url)
            .then(response => response.json())
            .then(data => {
                console.log('Fetched data:', data);
                $gameMessage.add('Data fetched successfully!');
            })
            .catch(error => {
                console.error('Error fetching data:', error);
                $gameMessage.add('Failed to fetch data.');
            });
    });
})();
JavaScript:
let status;
fetch(url)
.then(response =>  response.json().then(data => ({
    status: response.status,
    body: data})))
.then(obj => console.log(obj));

I'm not sure what it should get or read though, but maybe status must be included?
depending what it should show.
I imagine you've tested with an event like this?

◆Text:None, None, Window, Bottom : :Hi! I'm about to fetch some data! ◆Plugin Command:FetchExample, Fetch Data : :URL = https://jsonplaceholder.typicode.com/todos/1 ◆Text:None, None, Window, Bottom : :Hey, did it work?
In that case, if you've named the plugin file FetchExample.js (to match the registration in the code) then the command will run as intended.

However, fetch runs asynchronously, and the interpreter moves immediately to the next command (Show Text), so the $gameMessage.add call in your plugin will not display like you want it to. If you removed the "Hey, did it work?" message (and didn't activate any other event in the meantime) then this example would work as you expect.
Spoiler: technical explanation
The message window (Show Text) only displays one message at a time. The text to display is determined when the message "starts" (see the startMessage method of Window_Message in rmmz_windows.js); any additions during the message will technically be added to $gameMessage, but that won't affect the message being displayed.

The message is cleared when the window ends the current message (see the terminateMessage method, ibid). This means that if your fetch operation completes while another message is showing, your addition is effectively ignored.
A low-quality, unreliable workaround would be to simply add a command like Wait 30 frames after the command. It's a bad idea because you can't be sure that it will work correctly in edge cases.

I recommend you use something to let the interpreter know when to proceed. You could add a finally clause to your plugin command code for something like that. E.g.
  1. Implement a new wait mode:
    • Add a patch for the updateWaitMode method of Game_Interpreter, for your new wait mode.
    • Use this.setWaitMode("yourWaitModeName") before the fetch starts.
    • Make whatever change necessary in the finally clause to end your new wait mode.
    Note that arrow functions do not have a this value, so you'll also need to write your plugin command as a full function(args) { ... } for this approach to work. Plugin commands run in the context of the calling interpreter.

  2. Alternatively, turn a switch on (or off): you could then use the Loop, Branch, and Break Loop commands immediately after the plugin command to prevent further progress until that switch is toggled. That would be a somewhat clunky solution, but doesn't require any special knowledge to implement or customise.
Thank you for your replies and I'm sure they will help but apparently I'm having an issue with the plugin command even firing at all. I changed it to just log to the console and that doesn't ever show up. I've looked at other plugins that I know work and I don't see what I may have done wrong
I'm having an issue with the plugin command even firing at all. I changed it to just log to the console and that doesn't ever show up.
Before posting my previous reply, I tested your code, as-is, and it worked as I described. I've attached the file so you can try it for yourself~

I guess the problem is related to this:
if you've named the plugin file FetchExample.js (to match the registration in the code)
I.e. you may have saved the plugin with a different filename. Note that filenames are case-sensitive here, e.g. fetchExample.js and FetchExample.js are different files. The command uses the filename: if that doesn't match a registered function then the command will be skipped.

In case it helps: you can enter PluginManager._commands into the console to see all registered plugin commands.
Omg... I'm a little slow, sorry. I totally had a typo in my filename and didn't see that. Thank you so much for your help.
No worries, we all make mistakes! Good to hear it was something so easy to fix. 👋

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-load-data-from-url.173768/