简体   繁体   English

在循环内重复调用ajax直到满足条件

[英]Make repeated ajax calls inside loop until condition is met

I am calling an API which has pagination implemented.我正在调用一个实现了分页的 API。 The response from the API is来自 API 的响应是

{
  data {
    field1 : "value1",
    field2: "value2",
  },
  paginationKey : {
    id: "value for id",
    some_other_field: "value for other field"
  }
}

The value for the pagination key is specified in the request, and the response value for pagination key becomes the pagination key for the next request.分页键的值在请求中指定,分页键的响应值成为下一个请求的分页键。 The value for pagination key will be null for the first request, and the final value of pagination key in the response will be null.第一个请求的分页键值为空,响应中分页键的最终值为空。 So essentially I have to call the API with pagination key value of null, then whatever value of pagination key I get in response, use that for second request and keep continuing until the value of the key in the response becomes null.因此,基本上我必须使用分页键值为空来调用 API,然后我在响应中得到的分页键的任何值,将其用于第二个请求并继续继续,直到响应中键的值变为空。

My problem is I am making ajax call using JQuery to this API like我的问题是我正在使用 JQuery 对这个 API 进行 ajax 调用,例如

let ajaxPromise = $.ajax({
  url: requestUrl,
  type: 'GET',
  data: requestData, // containing the paginationKey like I mentioned above
  // other parameters for AJAX call like crossdomain, timeout etc
})

ajaxPromise.then(function(data) {
  successCallBack(data);
}, function(error, errorMessage) {
  failureCallBack(error, errorMessage)
})

with successCallBack and failureCallBack being methods that I have defined successCallBack 和 failureCallBack 是我定义的方法

Now the Ajax call, and the subsequent callbacks being asynchronous in JS, I am having difficulty to make these requests in a loop, and break out of this loop when the response paginationKey becomes null.现在 Ajax 调用和后续回调在 JS 中是异步的,我很难在循环中发出这些请求,并在响应 paginationKey 变为 null 时中断此循环。 How can I achieve this?我怎样才能做到这一点?

Since you need to wait for a call to finish before initiating a new call, you can use a standard loop.由于在启动新调用之前需要等待调用完成,因此可以使用标准循环。 A simple solution is to call the API once a call is done, and the key is not null :一个简单的解决方案是在调用完成后调用 API,并且 key 不为null

const repeatedAPICall = (requestData, successCallBack, failureCallBack) => {
  const ajaxPromise = $.ajax({
    url: requestUrl,
    type: 'GET',
    data: requestData, // containing the paginationKey like I mentioned above
    // other parameters for AJAX call like crossdomain, timeout etc
  })

  ajaxPromise.then((data) => {
    successCallBack(data)

    if(data.paginationKey) {
      repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
    }
  }, (error, errorMessage) => {
    failureCallBack(error, errorMessage)
  })
}

// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)

If you need an array of pages, you can use async/await in a for...of loop.如果您需要一组页面,您可以在for...of循环中使用async/await For every key that is not null , we add the key to the array.对于每个不为null的键,我们将键添加到数组中。 If there's a key in the array, we make an api call, and add the result to the results array.如果数组中有一个键,我们进行一个 api 调用,并将结果添加到results数组中。

 async function repeatedAPICall( apiCall, startValue ) { const keys = [startValue]; const result = []; for (const callKey of keys) { const data = await apiCall(callKey); result.push(data); if (data.key !== null) keys.push(data.key); } return result; } // mock api call const apiCall = ((counter) => () => Promise.resolve({ key: counter < 5 ? counter++ : null }))(0); repeatedAPICall(apiCall, null) .then(result => console.log(result)); // use your callbacks here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM