Answers for "rxjs"

2

rxjs

// Think of RxJS as Lodash for events.
import { Observable } from 'rxjs';
import { of } from 'rxjs';
import { first } from 'rxjs/operators';

const observable = new Observable(subscriber => {
  subscriber.next(1);
  subscriber.next(2);
  subscriber.next(3);
  setTimeout(() => {
    subscriber.next(4);
    subscriber.complete();
  }, 1000);
});

console.log('just before subscribe');
observable.subscribe({
  next(x) { console.log('got value ' + x); },
  error(err) { console.error('something wrong occurred: ' + err); },
  complete() { console.log('done'); }
});
console.log('just after subscribe');

/**
just before subscribe
got value 1
got value 2
got value 3
just after subscribe
got value 4
done
**/

of(1, 2, 3)
  .pipe(first())
  .subscribe((v) => console.log(`value: ${v}`));

// Logs:
// value: 1
Posted by: Guest on July-15-2021
-1

rxjs

content_copy
      
      
        open_in_new
      
      let count = 0;
let rate = 1000;
let lastClick = Date.now() - rate;
document.addEventListener('click', () => {
  if (Date.now() - lastClick >= rate) {
    console.log(`Clicked ${++count} times`);
    lastClick = Date.now();
  }
});
Posted by: Guest on January-26-2021
0

rxjs que recibe como parametro un observable

const $button = document.getElementById('main-button');const click$ = fromEvent($button, 'click');const subscription = click$.subscribe({  next: (e) => console.log('Event :', e)});// Luego de 5 segundos se cancela la suscripción a click$setTimeout(() => {  subscription.unsubscribe();}, 5000);
Posted by: Guest on December-30-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language