Is it possible to put a button within a function?

I’m pretty new to JS, but trying to learning all the time.
Some of the first code I wrote in the scripting blog has gotten supper hard to update, so thought to experiment to write small modules to keep divide up the code.

However it seems I can’t put buttons into functions?

let test = function () {

    let firstClick = await input.buttonsAsync(
    'What do you want to do?',
    {label: 'Check todays rate', value: 'today', variant: 'danger'},
    {label: 'Convert currency', value: 'convert', variant: 'primary'}

this gives me an error saying ‘SyntaxError: await is only valid in async function’
does theis mean my button can only be used in the global scope?

You can put buttons in functions. You just need to make the function an async function and await it when you call it.

let test = async function () {
  let firstClick = await input.buttonsAsync( ...

await test();

Sorry for the late reply, but didn’t completely understand it until I dug a bit deeper in JS and Async Await functions. Now it makes perfect sense.

1 Like

THANK YOU FOR THIS :slight_smile: I was pulling my hair out and now have it working!

1 Like