Help

Re: Is it possible to put a button within a function?

2353 2
cancel
Showing results for 
Search instead for 
Did you mean: 

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?

4 Replies 4

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.

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

cameronangeli
4 - Data Explorer
4 - Data Explorer

Hey All. Not sure if anyone has run into this, but it seems like use of 'variant' and 'varient' throwing inconsistent errors in my code. For example:  

This works: 

let q1Answer1 = {label:"One",value:1,varient:'primary'};
let q1Answer2 = {label:"Two",value:2,varient:'primary'};
question1 = await input.buttonsAsync("Question?",[q1Answer1,q1Answer2,])
 
This does not work: 
let q1Answer1 = {label:"One",value:1,variant:'primary'};
let q1Answer2 = {label:"Two",value:2,variant:'primary'};
question1 = await input.buttonsAsync("Question?",[q1Answer1,q1Answer2,])
 
This works: 
question1 = await input.buttonsAsync("Question?",[{label:"One",value:1,variant:'primary'},{label:"Two",value:2,variant:'primary'}])
 
This does not work: 
question1 = await input.buttonsAsync("Question?",[{label:"One",value:1,varient:'primary'},{label:"Two",value:2,varient:'primary'}])
 
Why is that happening?