Better practise is to use Observable
:
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
bbb() {
return this.http.get(this.url)
.pipe(
catchError(err => {
console.log('err', err);
return throwError('Something bad happened; please try again later.');
});
}
And, then simply just subscribe to bbb
:
import { Subscription } from 'rxjs';
testSubscription: Subscription;
ngAfterContentInit() {
this.testSubscription = this.bbb()
.subscribe(son => {
console.log('son', son); // here you get the result
});
}
Don't forget to unsubscribe
:
ngOnDestroy() {
this.testSubscription.unsubscribe();
}
If you still want to use Promise
:
bbb() {
return this.http.get(this.url).toPromise();
}
To get the result of bbb
:
ngAfterContentInit() {
this.bbb()
.then(son => {
console.log('son', son); // here you get the result
})
.catch(err => {
console.log(err);
});
}