Skip to content

Instantly share code, notes, and snippets.

@RichardSilveira
Created January 30, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardSilveira/705ba8ff7d3a643a2c1b7f8364f85194 to your computer and use it in GitHub Desktop.
Save RichardSilveira/705ba8ff7d3a643a2c1b7f8364f85194 to your computer and use it in GitHub Desktop.
/* Caso comum de uso dos Observables, sendo retornado por uma função que faz
chamadas assíncronas (requisições http).
Código exemplifica como é a criação "explícita" de um Observable,
mas existem outros métodos que fazem isso por você,
como Rx.Observable.of(<SeuObjetoRetornado>)*/
const obterRecursoDaApi = Rx.Observable.create(observer => {
setTimeout(() => {
observer.next([{id: 1, titulo: 'algum título'}, {id: 2, titulo: 'outro título'}]);
observer.complete();
}, 1000)
})
// Implementando apenas o callback 'next' do Observer
obterRecursoDaApi.subscribe(resp => console.log(JSON.stringify(resp)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment