-
-
Save uno-de-piera/50242df0f201bc9a612094695eabe2d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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