简体   繁体   English

angular2项目rxjs可观察到的错误

[英]angular2 project rxjs observable error

var bar = Observable.create(function(observer){
        try{
            console.log('hello');
            observer.next(22);
            throw new Error('bad bad bad');
            setTimeout(function(){
                observer.next(300);
                observer.complete();
            },2000);
        }catch(e){
            observer.error(e);
        }
    });
    bar.subscribe(
        function nextValueHandler(x){
            console.log(`out in handler${x}`);
        },
        function errorHandler(err){
            console.log('is wrong'+err);
        },
        function completeHandler(){
            console.log('over');
        }
    );

I am use rxjs api 5.0 in angular2 project . 我在angular2项目中使用rxjs api 5.0。 this code can be to error 'Unreachable code detected.' 此代码可能是错误“检测到无法访问的代码”。 but if make 'throw new error...' in 'setTimeout...' after it right , why can't make 'theow error...' in 'setTimeout...' before? 但是如果正确地在'setTimeout ...'之后执行'throw new error ...',为什么之前不能在'setTimeout ...'中执行'theow error ...'?

throw error will jump to catch block ignoring any code after it . throw error将跳转到catch块,忽略之后的任何代码。 Like return statement. return语句。 It's javascript behavior. 这是JavaScript行为。 So setTimeout is never called . 因此,永远不会调用setTimeout。

If you add it to setTimeout , it's callback so it won't check the code if it's unreachable or not . 如果将其添加到setTimeout,则它是回调的,因此它不会检查代码是否不可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM