是否可以 return new observable inside operator, nested observable
is it possible to return new observable inside operator, nested observable
var url = "http://domain.com/some/url";
// this endpoint will return websocket URL
Rx.DOM.ajax({
url: url,
method: 'GET',
responseType: 'json'
}).scan(function(o, data) {
// data.websocker is websocker url
return Rx.DOM.fromWebSocket(data.websocket_url, null, function(o) {
console.log(o);
}, function(o) {
console.log(o);
});
}).subscribe(function(data) {
// Success Message
console.log(data);
}, function(error) {
// Log the error
console.log(error);
});
如上例所示。是否可以像 promises 一样从操作员内部发送 Observable?
当然这很常见,并且有很多运算符以各种方式处理内部可观察量。在此示例中,您可以使用 flatMap
而不是 scan
:
var url = "http://domain.com/some/url";
// this endpoint will return websocket URL
Rx.DOM.ajax({
url: url,
method: 'GET',
responseType: 'json'
}).flatMap(function (data) {
// data.websocker is websocker url
return Rx.DOM.fromWebSocket(data.websocket_url, null, function(o) {
console.log(o);
}, function(o) {
console.log(o);
});
}).subscribe(function(data) {
// will receive messages that are received from the websocket
console.log(data);
}, function(error) {
// Log the error
console.log(error);
});
var url = "http://domain.com/some/url";
// this endpoint will return websocket URL
Rx.DOM.ajax({
url: url,
method: 'GET',
responseType: 'json'
}).scan(function(o, data) {
// data.websocker is websocker url
return Rx.DOM.fromWebSocket(data.websocket_url, null, function(o) {
console.log(o);
}, function(o) {
console.log(o);
});
}).subscribe(function(data) {
// Success Message
console.log(data);
}, function(error) {
// Log the error
console.log(error);
});
如上例所示。是否可以像 promises 一样从操作员内部发送 Observable?
当然这很常见,并且有很多运算符以各种方式处理内部可观察量。在此示例中,您可以使用 flatMap
而不是 scan
:
var url = "http://domain.com/some/url";
// this endpoint will return websocket URL
Rx.DOM.ajax({
url: url,
method: 'GET',
responseType: 'json'
}).flatMap(function (data) {
// data.websocker is websocker url
return Rx.DOM.fromWebSocket(data.websocket_url, null, function(o) {
console.log(o);
}, function(o) {
console.log(o);
});
}).subscribe(function(data) {
// will receive messages that are received from the websocket
console.log(data);
}, function(error) {
// Log the error
console.log(error);
});