gpt4 book ai didi

javascript - 在 rxjs Observable 中抛出错误

转载 作者:可可西里 更新时间:2023-11-01 01:31:22 25 4
gpt4 key购买 nike

我试图在 rxjs Observable 中抛出一个错误

 new Observable(subscriber => {
Observable.throw("error1");
return Observable.throw("error2");
})
.subscribe(
() => {},
err => console.error(err)
);

错误 1 ​​未被捕获。

error2 给出编译错误:

Argument of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => ErrorObservable<string>' is not assignable to parameter of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => TeardownLogic'. Type 'ErrorObservable<string>' is not assignable to type 'TeardownLogic'

在可观察对象中抛出错误的正确方法是什么?

最佳答案

使用Subscriber.error :

new Rx.Observable(subscriber => {
subscriber.error("error1");
subscriber.error("error2"); // not called because a stream will be finalized after any error
})
.subscribe(
() => {},
err => console.error(err)
);
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>

关于javascript - 在 rxjs Observable 中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com