聚合物iron-ajax和异步请求(etag同步和响应处理) [英] Polymer iron-ajax and asynchronous requests (etag sync and response handling)

查看:81
本文介绍了聚合物iron-ajax和异步请求(etag同步和响应处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种经过验证的方法来处理返回JSON的API端点的异步调用(使用聚合物最新的iron-ajax元素的再现)。这些API调用依赖于etag匹配,因此发送的etag与服务器上的etag匹配非常重要。除了在某些情况下,快速连续API调用可能会导致无序响应(因此可能导致etag不同步),我可以使用该部分。还有多个API端点(即不同的URL)。因此,有时如果通过iron-ajax元素启动使用不同端点的快速连续调用,则可能会导致响应处理函数出现问题,因为响应处理程序当前检查ajax元素的URL以了解如何正确处理响应。因此,如果第二个调用在收到第一个调用响应之前覆盖了ajax组件的URL,则当第一个调用确实返回时,responseHandler不会正确处理它。也许有更好更可靠的方法来确切地检查哪个电话已经返回?

I'm looking for a tried and true way of handling asynchronous calls to API endpoints returning JSON (using polymer's latest rendition of the iron-ajax element). These API calls rely on etag matching, so it is important that the etag sent matches the one on the server. I have that part working, except in certain circumstances, where quick succession API calls may cause an out-of-sequence response (and therefore can get the etag out of sync). There are also multiple API endpoints (i.e. different URLs). So, sometimes if quick succession calls using different endpoints are initiated via an iron-ajax element, it can cause issues for the response handler function, as the response handler currently checks the URL of the ajax element to know how to handle the response appropriately. Therefore, if the 2nd call overwrites the URL of the ajax component before the 1st call response is received, when the 1st call does come back the responseHandler doesn't handle it appropriately. Perhaps there is a much better and reliable way of checking exactly which call has returned?

我知道我不是第一个遇到这种情况的人,所以我是想知道是否有人可以向我展示开明的道路?我认为有一个简单的策略来处理这个问题,可能实现了呼叫排队等,但我不确定iron-ajax是否有任何内置可以帮助解决这个问题。

I know I'm not the first person to encounter this scenario, so I'm wondering if someone out there can show me the enlightened path? I'm thinking there is a simple strategy to handle this problem, perhaps implementing call queuing etc., but I'm not sure if iron-ajax has anything built-in that could help in that regard.

一些示例代码的示例绝对是惊人的!

An example with some sample code would be absolutely stupendous!

推荐答案

如果您依赖多个API对于每个端点,我会有一个单独的 iron-ajax 元素,这样如果URL发生变化(通过数据绑定或其他东西),它们就不会互相攻击:

If you depend on multiple API endpoints, I would have a separate iron-ajax element for each one so that they don't stomp on each other if the URLs change (through data-binding or something):

<iron-ajax id="cats" url="https://api.example.com/cats" handle-as="json"></iron-ajax>
<iron-ajax id="dogs" url="https://api.example.com/dogs" handle-as="json"></iron-ajax>

生成请求,并使用 Promise.all()等待两个请求完成:

Generate the requests, and use Promise.all() to wait for both requests to complete:

<script>
    Polymer({
        is: 'my-element',

        ...,

        fetchPets: function () {
            var catsRequest = this.$.cats.generateRequest();
            var dogsRequest = this.$.dogs.generateRequest();

            Promise.all([catsRequest.completes, dogsRequest.completes])
                .then(function (requests) {
                    var cats = requests[0].response;
                    var dogs = requests[1].response;

                    // do whatever you want from here...
                    alert(cats.concat(dogs));
                });
        }
    })
</script>

这篇关于聚合物iron-ajax和异步请求(etag同步和响应处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆