Observable 最终没有被解雇
Observable finally not getting fired
这个有效:
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",知道为什么吗?
不同之处在于,在第二个示例中,.finally
与 .subscribe
不在同一流中,因为您没有链接它们。
您的第一个示例创建此流:
get -> finally -> subscribe
你的第二个例子创建了两个分支:
get -> finally
get -> subscribe
如果没有订阅,finally 将不会执行。
如果要构建流,则需要使用前一个运算符的结果来链接运算符。它不像就地运算符。
这个有效:
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",知道为什么吗?
不同之处在于,在第二个示例中,.finally
与 .subscribe
不在同一流中,因为您没有链接它们。
您的第一个示例创建此流:
get -> finally -> subscribe
你的第二个例子创建了两个分支:
get -> finally
get -> subscribe
如果没有订阅,finally 将不会执行。
如果要构建流,则需要使用前一个运算符的结果来链接运算符。它不像就地运算符。