【发布时间】:2020-04-12 23:10:28
【问题描述】:
直接订阅从方法返回的 Observable 是一种好习惯吗?
在调用回调方法之前会不会被垃圾回收?
例如我有服务中的方法:
get<City>(id): Observable<City> {
var url = this.baseUrl + "api/Cities/" + id;
return this.http.get<City>(url);
}
然后在组件中我有:
ngOnInit () {
this.cityService.get<City>(this.id)
.subscribe(result => {
this.city = result;
},
error => console.error(error));
}
回调方法可以随时调用,甚至在 10 分钟之后,并且 Observable 已经超出范围。
那么有没有可能在不好的情况下,在回调方法中分配城市之前,返回的 Observable 可以被垃圾回收呢?
【问题讨论】:
-
我不这么认为。那会破坏
Promise。
标签: javascript angular rxjs garbage-collection observable