I’m currently evaluating the pros ‘n’ cons of replacing Angular’s resp. RxJS’ Observable
with plain Promise
so that I can use async
and await
and get a more intuitive code style.
One of our typical scenarios: Load some data within ngOnInit
. Using Observables
, we do:
ngOnInit () {
this.service.getData().subscribe(data => {
this.data = this.modifyMyData(data);
});
}
When I return a Promise
from getData()
instead, and use async
and await
, it becomes:
async ngOnInit () {
const data = await this.service.getData();
this.data = this.modifyMyData(data);
}
Now, obviously, Angular will not “know”, that ngOnInit
has become async
. I feel that this is not a problem: My app still works as before. But when I look at the OnInit
interface, the function is obviously not declared in such a way which would suggest that it can be declared async
:
ngOnInit(): void;
So -- bottom line: Is it reasonable what I’m doing here? Or will I run into any unforseen problems?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…