gpt4 book ai didi

angular - observer.error()之后需要调用observer.complete()?

转载 作者:太空狗 更新时间:2023-10-29 17:25:01 25 4
gpt4 key购买 nike

我正在用 Angular 2 编写一个可观察对象。我的代码是这样的:

Observable.create(observer => {
// fetched something from web service
if (some condition) {
observer.next('something');
observer.complete();
}
else { // error with no data
observer.error('something else');
observer.complete(); // Is this required here? Or can I skip this in case of error() ?
}
});

最佳答案

你不需要调用complete,它不会做任何事情,因为你已经调用了error

文档说:http://reactivex.io/documentation/observable.html

By the terms of the Observable contract, it may call onNext zero or more times, and then may follow those calls with a call to either onCompleted or onError but not both, which will be its last call

你可以看看源码:

关于angular - observer.error()之后需要调用observer.complete()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47220835/

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