Skip to content

Instantly share code, notes, and snippets.

@wirelessr
Last active February 11, 2022 05:41
Show Gist options
  • Save wirelessr/4e0032eb3beaca927bf864aec21bc53a to your computer and use it in GitHub Desktop.
Save wirelessr/4e0032eb3beaca927bf864aec21bc53a to your computer and use it in GitHub Desktop.
const events = require('events');
const emitter = new events.EventEmitter();
emitter.on('purchased', function(user, total) {
if (total >= 1000) {
giveCoupon(cart.user, total);
}
});
emitter.on('purchased', function(user, total) {
if (ok && total >= 5000) {
lottery(cart.user, total);
}
});
function purchase(cart) {
const total = 0;
for (let item of cart.items) {
total += item.price;
}
const ok = payByCreditCard(cart.user, total);
if (ok) {
emitter.emit('purchased', cart.user, total);
}
return ok;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment