Javascript Axios递归,用于使用光标对api进行分页

Javascript Axios递归,用于使用光标对api进行分页,javascript,node.js,recursion,cursor,axios,Javascript,Node.js,Recursion,Cursor,Axios,如何使用axios使用光标为API分页?我希望递归调用此函数,直到response.data.length{ //获得所有用户 }); } const getOne=(users=[])=>axios.get(“/users”) 。然后({data})=>{ //使用最终数组解析在“getUsers”中初始化的承诺: if(data.length

如何使用
axios
使用光标为API分页?我希望递归调用此函数,直到
response.data.length<1
,并在完成时返回包含集合中所有项的整个数组。另外,值得注意的是,我必须将光标传递到后续调用中

function getUsers () {
    return axios.get('/users') // API supports a cursor param (?after=)
             .then(response => {
               // returns an array with a cursor
               // see response below
               console.log(response.data)
             })
}
答复示例:

{
    "total": 100,
    "data": [
        {
            user: "Bob"
        },
        {
            user: "Sue"
        },
        {
            user: "Mary"
        },
    ],
    "pagination": {
        "cursor": "lkdjsfkljsdkljfklsdjfkjsdk"
    }
}
function getUsers (cursor, data = []) {
    return axios.get('/users' + (cursor ? '?after='+cursor : '')) // API supports a cursor param (?after=)
      .then(response => {
          if (response.data.length < 1 ) return data
          data.push(...response.data)
          return getUsers(response.pagination.cursor, data)
      })
}
// use it to get data
getUsers()
.then(data => console.log("final data", data))

提前感谢您的帮助。

有一个单独的递归函数,它接受一个数组,获取
/users
,或者推送到数组并再次调用自身,或者在没有其他用户时解析:

function getUsers () {
  getOne()
    .then((users) => {
      // got all users
    });
}

const getOne = (users = []) => axios.get('/users')
  .then(({ data }) => {
    // Resolve the promise initialized in `getUsers` with the final array:
    if (data.length < 1) return users;
    // Else, push to the array and chain another promise onto the end:
    users.push(...data);
    return getOne(users);
  })
函数getUsers(){ getOne() 。然后((用户)=>{ //获得所有用户 }); } const getOne=(users=[])=>axios.get(“/users”) 。然后({data})=>{ //使用最终数组解析在“getUsers”中初始化的承诺: if(data.length<1)返回用户; //否则,推到阵列并将另一个承诺链到末端: push(…数据); 返回getOne(用户); })
这里有一个递归函数,用于管理响应中的光标:

{
    "total": 100,
    "data": [
        {
            user: "Bob"
        },
        {
            user: "Sue"
        },
        {
            user: "Mary"
        },
    ],
    "pagination": {
        "cursor": "lkdjsfkljsdkljfklsdjfkjsdk"
    }
}
function getUsers (cursor, data = []) {
    return axios.get('/users' + (cursor ? '?after='+cursor : '')) // API supports a cursor param (?after=)
      .then(response => {
          if (response.data.length < 1 ) return data
          data.push(...response.data)
          return getUsers(response.pagination.cursor, data)
      })
}
// use it to get data
getUsers()
.then(data => console.log("final data", data))

谢谢你。这似乎需要进行一些修改,主要围绕
axios
的工作方式和API的形状
data.push(…response.data.data)
return-getUsers(response.data.pagination.cursor,data)Cool@TechnoTim至少指向了正确的方向。谢谢你。我将接受Mark的回答,因为它包含光标的逻辑。谢谢你的帮助!