简体   繁体   中英

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 . 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?

throw error will jump to catch block ignoring any code after it . Like return statement. It's javascript behavior. So setTimeout is never called .

If you add it to setTimeout , it's callback so it won't check the code if it's unreachable or not .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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