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:
Hook outside my class where the getButton function is used:
Any help would be appreciated.
Cheers
Solution:Jump to 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();...2 Replies
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.
Thank you for your help, I tried what you mentioned and it fixed my problem 🙂