Observables
provide a more flexible approach compared to handling individual events like clicks. There are numerous functions available for use with Observables, making it beneficial to explore the documentation yourself.
If you only need to handle basic click events, using button click may suffice. However, if you require complex manipulations and logic such as delaying an event or merging with API calls before displaying data, checking out the Observables documentation is recommended for methods that can aid in implementing your logic.
The order of execution when three different approaches are applied:
const button = $('#btn');
document.getElementById('btn').addEventListener('click', () => console.log('From pure callback'));
Rx.Observable.fromEvent(button, 'click')
.subscribe(() => console.log('From observable'));
button.on('click', () => console.log('From jQuery callback'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='https://unpkg.com/@reactivex/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1062687a6350253e243e23">[email protected]</a>/dist/global/Rx.js'></script>
<button id='btn'>Click</button>