So I been trying to understand how to get value from Promise that is returned by function Alert.present(). As per the image, writing awaits throws me an error. On the other hand removing awaits returns {}.
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()
•
u/AutoModerator Aug 25 '21
Thanks for the submission!
It looks like you may have not shared the code you want help with.
Please be sure to include that. If you did, then you can safely ignore this comment.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/nilayperk Aug 25 '21
So I been trying to understand how to get value from
Promisethat is returned by functionAlert.present(). As per the image, writingawaitsthrows me an error. On the other hand removingawaitsreturns{}.