Javascript 使用.subscribe()处理错误
我使用rxjs5测试版来加载图像和做其他事情。但是,我不明白为什么Javascript 使用.subscribe()处理错误,javascript,rxjs,Javascript,Rxjs,我使用rxjs5测试版来加载图像和做其他事情。但是,我不明白为什么.subscribe()不能处理404错误。是因为事件不能失败吗?这是我的密码: var image = new Image(); image.src = 'http://some.file/that/does/not-exist.jpg'; var sourceLoad = Rx.Observable.fromEvent(image, 'load'); var sourceError = Rx.Observable.fromE
.subscribe()
不能处理404错误。是因为事件不能失败吗?这是我的密码:
var image = new Image();
image.src = 'http://some.file/that/does/not-exist.jpg';
var sourceLoad = Rx.Observable.fromEvent(image, 'load');
var sourceError = Rx.Observable.fromEvent(image, 'error');
sourceLoad.subscribe(
function(e) {
console.log('loaded');
}/*,
function(e) {
console.log('Handling errors here is not working. Why?')
}
*/
);
sourceError.subscribe(
function(e) {
console.log('error');
}
);
试试这个:
var sourceLoad = Rx.Observable.from([1,2]).map(x => throw 'error here')
sourceLoad.subscribe(
x => console.log('A OK'),
err => console.log(err)
)
ES5在这里摆弄:也许您没有收到错误?我甚至想提到这一点,但可能是因为错误处理程序被注释掉了?请记住,
sourceError
subscriber实际上并没有记录错误,而是记录成功。subscribe函数的第二个参数是错误处理程序。