Skip to content

Instantly share code, notes, and snippets.

@simaodeveloper
Last active July 3, 2020 18:33
Show Gist options
  • Save simaodeveloper/4b2802a18bdd568c60a280c77fde160b to your computer and use it in GitHub Desktop.
Save simaodeveloper/4b2802a18bdd568c60a280c77fde160b to your computer and use it in GitHub Desktop.
Observable Study - RxJS - https://out.stegrider.now.sh/
const { fromEvent } = Rx;
const { map, pluck } = RxOperators;
const input = document.createElement('input');
const container = document.querySelector('.container');
container.appendChild(input)
const observable = fromEvent(input, 'input')
.pipe(
//map(event => event.target.value),
pluck('target', 'value'),
map(value => parseInt(value)),
map(value => {
if(isNaN(value)) {
throw new Error('Inform a number!');
}
return value;
})
);
// observer
observable.subscribe({
next(value) {
console.log(`Your value is ${value}`);
},
error(err) {
console.error(`BAD THING HAPPEN!!!`, err.message);
},
})
observable;
const { Observable } = Rx;
const observable = new Observable((subscriber) => {
// Throw the value 1 into our pipeline
subscriber.next(1)
// complete e finish all flow, no more values will come out
subscriber.complete();
// emit an error, no more values will come out
subscriber.error(new Error('Error Notification!'));
});
observable.subscribe({
next(value) {
console.log(value);
},
complete() {
console.log('complete');
},
error(err) {
console.log('ERROR', err.message);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment