Skip to content

Instantly share code, notes, and snippets.

@uno-de-piera uno-de-piera/User.ts Secret
Created Jan 21, 2018

Embed
What would you like to do?
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/fromPromise';
class User {
userPromise() {
return new Promise((resolve, reject) => {
if(1 === 1) {
return resolve([{name: 'Iparra'}])
}
return reject({msg: 'error'});
})
}
promiseToObservable() {
return Observable.fromPromise(this.userPromise())
.map(user => user[0])
.catch(error => Observable.throw(err));
}
}
let user = new User();
user.promiseToObservable().subscribe(
result => {
console.log(result); //[{name: 'Iparra'}]
},
error => {
console.log(error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.