javascript - .then() 异步中的 While 循环
问题描述
我最近遇到了一个有趣的问题,我一生都无法解决。我正在调用将记录分解为页面的 API 调用,页面的信息驻留在响应标头中。从那以后,我希望能够再次调用来检索数据和下一个标头,直到没有更多的响应标头。
let parents = {};
const options = {
credentials: "same-origin",
headers: {
accept: "application/json"
},
timeout: 5000
};
fetch(
`/api/v1/courses/200003/enrollments?enrollment_type=ObserverEnrollment&per_page=100`,
options
).then(response =>
response
.json()
.then(data => ({
data: data,
ok: response.ok,
headers: response.headers
}))
.then(res => {
parents = res;
nextURL(res.headers.get("Link"));
let optionsNext = {
credentials: "same-origin",
headers: {
accept: "application/json"
},
timeout: 5000
};
fetch(nextURL(res.headers.get("Link")), optionsNext).then(response =>
response
.json()
.then(data => ({
data: data,
ok: response.ok,
headers: response.headers
}))
.then(res => {
if (res.ok) {
parents.data.push(res.data);
console.info(parents);
}
})
);
})
);
function nextURL(linkTxt) {
let url = null;
if (linkTxt) {
let links = linkTxt.split(",");
let nextRegEx = new RegExp('^<(.*)>; rel="next"$');
for (let i = 0; i < links.length; i++) {
let matches = nextRegEx.exec(links[i]);
if (matches) {
url = matches[1];
}
}
}
return url;
}
我需要放入某种循环的部分是基于 nextURL 函数的返回的二次提取:if !nextURL(res.headers.get("Link"))
我需要break
循环。
let optionsNext = {
credentials: "same-origin",
headers: {
accept: "application/json"
},
timeout: 5000
};
fetch(nextURL(res.headers.get("Link")), optionsNext).then(response =>
response
.json()
.then(data => ({
data: data,
ok: response.ok,
headers: response.headers
}))
.then(res => {
if (res.ok) {
parents.data.push(res.data);
console.info(parents);
}
})
);
提前感谢您甚至看我可怜的问题
解决方案
尝试使用递归;像这样的东西:
const getFetcher = () => ({
aggregate: [],
run: function (url, options) {
return new Promise((resolve, reject) => {
fetch(url, options)
.then(response => {
const json = response.json();
const { headers, data } = response;
const nextLink = res.headers.get("Link");
this.aggregate.push(data);
if (nextLink) {
this.run(nextLink, options).then(resolve);
}
else {
resolve(this.aggregate);
}
})
})
}
})
const options = {
credentials: "same-origin",
headers: {
accept: "application/json"
},
timeout: 5000
};
const fetcher = getFetcher();
fetcher.run(`/api/v1/courses/200003/enrollments?enrollment_type=ObserverEnrollment&per_page=100`, options)
.then(allPagesData => {
/* at this point you have all the pages data */
})
推荐阅读
- python - 探索 Spark 执行计划、阶段数等
- kubernetes - 有没有办法挂钩 Kubernetes 入口资源更改事件?
- java - 在自定义验证器中访问父属性路径
- python - 根据 std 过滤多索引 df
- android - 连接充电器时运行代码后台
- amazon-web-services - 需要为 3 个云提供商 AWS、GCP、阿里巴巴创建集中式 DNS/LDAP
- google-apps-script - 将一行内容导出到具有一定结构的文本文件中
- robotframework - 无法从测试用例运行 shell 脚本
- python - 将表导入 DataFrame 并将列组设置为列表
- python - 使用 BeautifulSoup 抓取 Tripadvisor 评论时出现 ValueError