您的位置:首页 > Web前端 > JavaScript

[RxJS] Creation operators: empty, never, throw

2016-04-14 01:06 585 查看
This lesson introduces operators empty(), never(), and throw(), which despite being plain and void of functionality, are very useful when combining with other Observables.

empty:

var foo = Rx.Observable.empty();
// the same as
var foo = Rx.Observable.create( function(observe) {
observe.complete();
})

foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});

//done


never(): do nothing

var foo = Rx.Observable.never();
// the same as
var foo = Rx.Observable.create( function(observe) {
})

foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});


throw(): throw a Javascript error

var foo = Rx.Observable.throw(new Error('blooom'));
// the same as
var foo = Rx.Observable.create( function(observe) {
observe.error('bloom');
})

foo.subscribe(function (x) {
console.log('next ' + x);
}, function (err) {
console.log('error ' + err);
}, function () {
console.log('done');
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: