我的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.
更多推荐
发布评论