Given I have the following function:
async function foo() {
document.getElementById('ok').addEventListener('click', () => {
// How do I resolve from foo here?
})
document.getElementById('cancel').addEventListener('click', () => {
// How do I reject from foo here?
})
}
As the comment states, how can I resolve or reject foo's promise within the nested function?
My current solution is:
function foo() {
return new Promise((resolve, reject) => {
document.getElementById('ok').addEventListener('click', () => {
resolve()
})
document.getElementById('cancel').addEventListener('click', () => {
reject()
})
})
}
But with this, I have to return the Promise myself rather than using async
. Is there a better way?
Edit: The example I gave initially was not the best. Updated my question and code.