如何在响应时链接 axios 获取请求更多标志为真



我正在访问一个端点,该端点为我提供了用户列表,但结果是分页的。响应包含一个标志hasMore,该标志告知是否有更多用户要检索,以及要进行下一次 api 调用的offset

现在,我可以通过手动检查结果hasMore是否为真来进行多次调用。如何将此逻辑包装在 while 循环中?

function getUsers() {
let users = [];

axios.get(url)
.then(res => {
res.users.forEach(user => {
users.push(user);
})

if (res.hasMore) {
return axios.get(url + '&offset=' + res.offset)
}
})
.then(res => // repeat what I've just done and keep checking hasMore
// How do I check this in a while?

}

你能把users = []提升一个级别吗?

let users = [];
function getUsers(url) {
axios
.get(url)
.then(res => {
res.users.forEach(user => {
users.push(user);
})
if (res.hasMore) {
getUsers(url + '&offset=' + res.offset);
}
})
.catch(err => {...});
}

最新更新