使用RxJS将几个ajax请求转换为Observable

编程入门 行业动态 更新时间:2024-10-24 22:26:54
本文介绍了使用RxJS将几个ajax请求转换为Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力争取某些东西 - 我猜这意味着我误解了并且做了些傻事

I'm struggling with something - which I'm guessing means I've misunderstood and am doing something silly

我有一个可观察的,需要用它来创建一些对象,将其发送到服务器进行处理,将服务器的结果与我发送的对象组合,然后将其转换为可观察对象,这样我想做的事(我认为)就像是

I have an observable and need to use it to create some object, send that to the server for processing, combine a result from the server with the object I sent, and then turn that into an observable so what I want to do (I think) is something like

var theNewObservable = my.observable.things.select(function(thing) { var dataToSend = generateMyJavascriptObjectFrom(thing); var promise = $.ajax({ type: 'POST', url: somewhere, data: dataToSend }).promise(); return rx.Observable.fromPromise(promise).subscribe(function(data, status, jqXHR) { var infoFromServer = jqXHR.getResponseHeader('custom-header-returned'); // I'm wanting this to be the thing other code can subscribe to return { infoFromServer: dataToSend }; }, function(err) { alert('PC LOAD LETTER!'); console.error(err); }); } }); theNewObservable.subscribe(function(combinedInfo) { console.log(combinedInfo) };

我期待 {infoFromServer:dataToSend} 我得到一个 AutoDetachObserver 我可以看到它有一个带有ajax onSuccess签名的onNext,所以我显然做了一些愚蠢的事情

where I'm expecting {infoFromServer: dataToSend} I'm getting an AutoDetachObserver and I can see that has an onNext with the ajax onSuccess signature so I'm obviously doing something silly

推荐答案

一些应该有所帮助的事情:

A couple things that should help a bit:

1) subscribe 方法是一种终端方法,因为它不会返回任何内容。这是 Observer 附加的地方,因此在 subscribe

1) The subscribe method is a terminal method, as in, it won't return anything. It is where the Observer attaches so there should be no further data propagation after the subscribe

2)订阅的 onNext 方法只能取一个值需要包含所有的消息数据。

2) The onNext method of subscribe can only take a single value which you will need to have all the message data wrapped in.

由于jQuery的 Promise 对此行为不会很好,你有两种选择。首先,您可以使用 RX-DOM 项目获取 Observable ajax版本。或者你需要包装promise方法。如果您还需要等待响应,则应使用 selectMany ,这将允许您触发承诺,然后等待其返回并将响应映射到原始请求。

Since jQuery's Promise will not behave well with this, you have two options. First, you can use the RX-DOM project for an Observable ajax version. Or you will need to wrap the promise method. If you further need to wait on the response you should be using selectMany instead, which will allow you to fire off the promise, then await its return and map the response to the original request.

var theNewObservable = my.observable.things //Preprocess this so that `selectMany` will use //dataToSend as the request object .map(function(thing) { return generateMyJavascriptObjectFrom(thing); }) .selectMany(function(dataToSend) { var promise = $.ajax({ type: 'POST', url: somewhere, data: dataToSend }).promise(); //Rewrap this into a promise that RxJS can handle return promise.then(function(data, status, jqXHR) { return {data : data, status : status, jqXHR : jqXHR}; }); }, function(request, response) { return { infoFromServer : response.jqXHR.getResponse('custom-header'), dataToSend : request }; }); theNewObservable.subscribe( function(combinedInfo) { console.log(combinedInfo) }, function(err) { alert('PC LOAD LETTER!'); console.error(err); });

更多推荐

使用RxJS将几个ajax请求转换为Observable

本文发布于:2023-11-24 19:50:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   转换为   RxJS   Observable   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!