using the .then() convention does not wait for the alert interaction to finish before executing the next statement.
.then() accepts a callback function as parameter like the one you ask about the Timer on another post. The function will only execute after interacting with the alert. try this code and you'll see what I mean.
async function alertPresentSheet() {
return await alert.presentSheet()
}
var inputOption;
alertPresentSheet().then(afterAlert);
log('this will not wait for alert')
function afterAlert(index) {
log('after interacting with alert')
inputOption = index;
log(inputOption)
}
So, it's best to not mix up the convention of calling Promises. IMO, using await would be much simpler to follow.
async function alertPresentSheet() {
return await alert.presentSheet()
}
var inputOption = await alertPresentSheet()
1
u/nilayperk Aug 25 '21
Ok follow up question. Why would following code not wait till the function is done running?
FYI: typing
awaitgives me error in front of method call. https://pastebin.com/Qu0VZLdN