Rxjs 可观察到最终没有被解雇

Rxjs 可观察到最终没有被解雇,rxjs,angular2-http,Rxjs,Angular2 Http,这项工作: this.http.get('/doesntexist1') .finally(() => console.log('finally1')) .subscribe(() => { }); 但这并不是: const obs = this.http.get('/doesntexist2'); obs.finally(() => console.log('finally2')) obs.subscribe(() => { }); 两个URL都生成404

这项工作:

this.http.get('/doesntexist1')
  .finally(() => console.log('finally1'))
  .subscribe(() => { });
但这并不是:

const obs = this.http.get('/doesntexist2');
obs.finally(() => console.log('finally2'))
obs.subscribe(() => { });
两个URL都生成404


我同时运行这两个命令,我只看到控制台中显示“finally1”,知道为什么吗?

区别在于,在第二个示例中,
。最后,
。subscribe
不在同一个流中,因为您没有链接它们

您的第一个示例创建此流:

get -> finally -> subscribe
第二个示例创建了两个分支:

get -> finally
get -> subscribe

finally将在没有订阅的情况下执行


如果要构建流,则需要使用前一个操作符的结果来链接操作符。它不像一个就地操作符。

“finally不会在没有订阅的情况下执行。”你知道为什么吗?@Luke streams在没有订阅的情况下不会执行。