Consider the following case:
const waitForEvent = async (api) => {
api.on('eventOne', () => {
return 'eventOne';
})
api.on('eventTwo', () => {
return 'eventTwo';
})
api.on('eventThree', () => {
return 'eventThree';
})
api.load();
}
What I am trying to do is setup event callbacks on the api
variable inside the async function, trigger the api.load()
function, and then return the event that happened first, in this case either eventOne|eventTwo|eventThree
Problem is, this syntax is bad, and this example does not work. I couldn't find any way to achieve this using async/await and had to revert to promises like this:
const waitForEvent = (api) => {
return new Promise(resolve) => {
api.on('eventOne', () => {
resolve('eventOne');
})
api.on('eventTwo', () => {
resolve('eventTwo');
})
api.on('eventThree', () => {
resolve('eventThree');
})
api.load();
}
}
So my question is, can this be accomplished using async/await? Anyway this can be done using the new async/await es7 syntax?
return
statements are returning their value from their specific event handler functions, not the mainwaitForEvent
function – Mortgageeasync
/await
uses promises anyway? And ifapi
is not promisified, you need to use thePromise
constructor. – Heerlen