Disallowed operation called within global scope...

I'm trying to call an async API wrapper within a worker, but whenever I try to add the
stats
function, I get "isallowed operation called within global scope. Asynchronous I/O (ex: fetch() or connect()), setting a timeout, and generating random values are not allowed within global scope". Unless I'm misinterpreting the documentation, is this not out of global scope (and inside a handler)?


import { verifyDiscordRequest } from './verifyDiscordRequest';
import Client from "mylib"

addEventListener('fetch', (event) => {
    event.respondWith(handleRequest(event.request));
});

async function handleRequest(request: Request<unknown>, CfProperties<unknown> ) {

    if (commandName === 'ping') {
    } else if (commandName === 'stats') {
        return await stats(interaction).then((data) => {
            return new Response(JSON.stringify(data.base_info), {
                headers: { 'Content-Type': 'application/json' },
            });
        });
    }

    function stats(interaction: any) {
        return new Client({
            loginOptions: {
            },
        }).getStats(interaction.data?.options[0]?.value);
    }
}
Was this page helpful?