Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samueleresca/c926bb8e06c28c32bbf54c37c8cf0cd5 to your computer and use it in GitHub Desktop.
Save samueleresca/c926bb8e06c28c32bbf54c37c8cf0cd5 to your computer and use it in GitHub Desktop.
//example promise that will resolve or reject based on input
const myPromise = (willReject) => {
return new Promise((resolve, reject) => {
if(willReject){
reject('Rejected!');
}
resolve('Resolved!');
})
}
//emit true, then false
const source = Rx.Observable.of(true, false);
const example = source
.mergeMap(val => Rx.Observable
//turn promise into observable
.fromPromise(myPromise(val))
//catch and gracefully handle rejections
.catch(error => Rx.Observable.of(`Error: ${error}`))
)
//output: 'Error: Rejected!', 'Resolved!'
const subscribe = example.subscribe(val => console.log(val));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment