How we can end an async
function from a callback passed to an event emitter without promisifing the event emitter?
Also without using <
Can we end an async function from a callback passed to an event emitter without promisifing the event emitter?
No. async
/await
syntax is just sugar for then
calls and relies on promises.
async function bakeMeSomeBurgers () {
let canIHave = await canIHazACheezBurger();
if (canIHave)
console.log('Hehe, you can have...');
else
console.log('NOPE');
// Here we create and await our promise:
await new Promise((resolve, reject) => {
// Here invoke our event emitter:
let cook = new BurgerCooking('cheez');
// a normal event callback:
cook.on('update', percent => {
console.log(`The burger is ${percent}% done`);
});
cook.on('end', resolve); // call resolve when its done
cook.on('error', reject); // don't forget this
});
console.log('I\'ve finished the burger!');
if (canIHave)
console.log('Here, take it :)');
else
console.log('Too bad, you can\'t have it >:)');
}