如何取消旧的承诺?(How to cancel old promises?)

编程入门 行业动态 更新时间:2024-10-21 15:44:19
如何取消旧的承诺?(How to cancel old promises?)

我的Ionic 3应用程序的一个页面进行网络呼叫并以承诺的形式接收结果。 但有些电话比其他电话需要更多时间。 在某些情况下,在请求B之前发出的请求A可能比请求B花费更多时间来响应。

例如,在文本查询搜索中,如果用户使用后端的不同参数触发两个后续请求,则他可以在响应A之前接收响应B.然后视图将显示第二个搜索结果,然后在一段时间之后,第一个搜索结果,这不是预期的行为。 我总是需要显示最后一个请求的数据。

我怎么能取消一个承诺,以便我离开显示最新的数据?

A page of my Ionic 3 app make network calls and receive results in the shape of a promise. But some calls takes more time than others. In some cases the request A, which is made before request B, can take more time to response than request B.

For example, in a text query search, if the user triggers two subsequent requests with different parameters for the backend he can receive the response B before the response A. Then the view will show the second search result and then,after some time, the first search result, which is not the expected behavior. I always need to show the data of the last request.

How could I cancel a promise so that I aways shows the most recent data?

最满意答案

我处理多个请求的竞争条件的方法是在请求中添加时间戳作为查询参数或标题,然后在收到响应时再次检查。 如果时间戳不是已经处理的最新时间戳,则将其丢弃。

The way I handled the race condition of multiple requests is to add a timestamp onto the request as a query parameter or a header, which you then check again when you receive the response. If the timestamp isn't the most recent that has been processed already, just throw it out.

更多推荐

本文发布于:2023-08-05 08:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:cancel   promises

发布评论

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

>www.elefans.com

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