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);
}
}
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);
}
}
2 Replies
Walshy
Walshy2w ago
It'll be reporting it from one of the imports
llamaz
llamazOP2w ago
So how do I import my library then? solved, using await import() worked.

Did you find this page helpful?