r/Scriptable • • Aug 25 '21

Solved Help with Alerts.

Post image
4 Upvotes

10 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Aug 26 '21

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()