gpt4 book ai didi

javascript - 从 API 获取分页的项目列表

转载 作者:行者123 更新时间:2023-12-03 02:33:34 25 4
gpt4 key购买 nike

我需要从 API 端点 (/products) 获取项目列表,但它们是分页的(每页最多 200 个项目)。

我需要创建一个循环,获取 200 个产品,推送到一个数组,并增加页码,这样它就可以请求更多 200 个产品。当出现 404 错误(页面不存在)时它将停止,这意味着我获得了所有产品。

我使用 Axios 来处理请求,它是基于 Promise 的,但我无法让它工作。我尝试了几种方法,甚至创建了自己的 Promise,但结果是相同的:

  • 无法让它等待所有页面被请求
  • 它将始终请求相同的页面,因为页面增量位于 promise 的 .then 内(以确保我不会超出最后一页)

我知道 Promises 的想法是异步的,但我正在尝试找到一种方法让它发挥作用。

有人知道我可以使用什么逻辑来做到这一点吗?我只想在继续之前获得所有元素。也许我太复杂了,一些澄清会有很大帮助。

编辑:

尝试递归进行,但结果总是在执行之前返回:

module.exports = {
sync(req, res) {
// Get all products page by page
var products = module.exports.getProductsByPage()
res.status(200).send(products)
},

getProductsByPage(page = 1, products = []) {
nuvemshop.get(`products?page=${page}&per_page=200`)
.then(res => {
console.log('GET Products PAGE ' + page)
products.push(res.data)
arguments.callee(++page, products)
})
.catch(e => {
if(e.response.status === 404) {
console.log('LAST PAGE REACHED')
return products
} else
return e
})
},

最佳答案

以下操作是否有效,或者是否会产生错误/意外结果?

const getProductsByPage = (page = 1, products = []) => 
//you need to return the promise here, arrow without {} block
// returns the single statement (x=>y = function(x){return y;})
nuvemshop.get(`products?page=${page}&per_page=200`)
.then(res => {
console.log('GET Products PAGE ' + page);
//you need to return the promise here
// call recursively
return getProductsByPage(
page+1,
products.concat(res.data)
);
})
.catch(e => {
if (e.response.status === 404) {
console.log('LAST PAGE REACHED')
return products
} else
return e
});

const sync = (req, res) => {
// Get all products page by page
var products = getProductsByPage()
.then(
products=>
res.status(200).send(products)
).catch(
err =>
res.status(500).send(err)
);
};


module.exports = {
sync
}

以下版本将一次获取 10 页,而不是一页一页地获取。如果出现问题,它将生成 Fail 类型结果,并删除 404 响应的 Fail 类型,但将保存任何其他失败原因:

const Fail = function(reason){this.reason = reason;};
const isFail = x=>(x&&x.constructor)===Fail;
const isNotFail = x=>!isFail(x);
const getProductsByPage = (pagesInSet=10) => {
//set up max 3 requests per second
//const max3 = throttlePeriod(3,1000);
const recur = (startAt,products) =>
Promise.all(
Array.from(new Array(pagesInSet),(_,index)=>index+startAt)
.map(
page=>
//for throttled
//max3(nuvemshop.get.bind(nuvemshop))(`products?page=${page}&per_page=200`)
nuvemshop.get(`products?page=${page}&per_page=200`)
.catch(
e=>new Fail(e,page)
)
)
).then(
resultSet=>{
//check if any results came back with 404
const needStop = resultSet
.filter(isFail)
.filter(
fail=>{
const [e] = fail;
return e.response.status === 404;
}
);
if(needStop.length!==0){
const without404 = products.concat(
resultSet.filter(
result=>{
if(isFail(result)){
const [e] = result;
return e.response.status !== 404;
}
return true;
}
)
);
return without404;
}
//get the next pagesInSet pages
return recur(startAt+pagesInSet,products.concat(resultSet));
}
);
return recur(1,[]);
}
const sync = (req, res) => {
// Get all products in sets of 10 parallel requests
var products = getProductsByPage(10)
.then(
products=> {
//you may want to do something with failed request (non 404)
//const failed = products.filter(isFail)
res.status(200).send(products);
}
).catch(
err =>
res.status(500).send(err)
);
};

module.exports = {
sync
}

关于javascript - 从 API 获取分页的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com