Error message on Roll: await is only valid in async functions and the top level bodies of modules

I am trying to get the .total value on a roll but I am not able to. I know Roll is async and you have to await for the roll to be finalised before I can get the total but I get this error message on the console when I try to use await: await is only valid in async functions and the top level bodies of modules This is my code: function on the main class of my module:
static getButton(userID){

let _button = document.createElement("button");
_button.className = "turn-icon-button flex0";
_button.title = "test";

//d20 icon
let _icon = document.createElement("i");
_icon.className = "fa-solid fa-dice-d20";

_button.append(_icon);

_button.addEventListener("click",function(event){
let r = new Roll("1d20");
//await r.toMessage();
//await r.evaluate();
r.toMessage();
console.log(r.total)
});

return _button;

}
static getButton(userID){

let _button = document.createElement("button");
_button.className = "turn-icon-button flex0";
_button.title = "test";

//d20 icon
let _icon = document.createElement("i");
_icon.className = "fa-solid fa-dice-d20";

_button.append(_icon);

_button.addEventListener("click",function(event){
let r = new Roll("1d20");
//await r.toMessage();
//await r.evaluate();
r.toMessage();
console.log(r.total)
});

return _button;

}
Hook outside my class where the getButton function is used:
Hooks.on('renderPlayerList', (playerList, html) => {
if (game.user.isGM){
const loggedInUserList = html.find(".player");
for (var i = loggedInUserList.length - 1; i >= 0; i--) {
loggedInUserList[i].append(mymodule.getButton(loggedInUserList[i].getAttribute("data-user-id")))
}
}
Hooks.on('renderPlayerList', (playerList, html) => {
if (game.user.isGM){
const loggedInUserList = html.find(".player");
for (var i = loggedInUserList.length - 1; i >= 0; i--) {
loggedInUserList[i].append(mymodule.getButton(loggedInUserList[i].getAttribute("data-user-id")))
}
}
Any help would be appreciated. Cheers
Solution:
That error is referring to the fact that the function you're giving in addEventListener isn't async. If you add an await to a function, that function needs to be labeled as async. Something like this would fix that. ```js _button.addEventListener("click", async function(event){ let r = new Roll("1d20"); await r.toMessage();...
Jump to solution
2 Replies
Solution
kaelad
kaelad•5d ago
That error is referring to the fact that the function you're giving in addEventListener isn't async. If you add an await to a function, that function needs to be labeled as async. Something like this would fix that.
_button.addEventListener("click", async function(event){
let r = new Roll("1d20");
await r.toMessage();
console.log(r.total)
});
_button.addEventListener("click", async function(event){
let r = new Roll("1d20");
await r.toMessage();
console.log(r.total)
});
Ark
ArkOP•5d ago
Thank you for your help, I tried what you mentioned and it fixed my problem 🙂

Did you find this page helpful?