题目 调取有分页的API 说明 你是否遇到过一些有分页的API,然后需要按顺序进行调用的情况?
假设我们又一个 /list API,这个API返回 items数组。
// fetchList 已经提供给你了,直接可用
const fetchList = (since?: number) =>
Promise<{items: Array<{id: number}>}>
- 第一个request,直接调用
fetchList,从response中取得最后一个item的id - lastItemId
2.调用fetchList(lastItemId)获取下一个response 3.重复上述过程
/list API 一次只返回5个item,加上一些过滤,实际的返回值中可能比5更少。如果一个都没有返回的话,就意味着服务器已经没有可以返回的了,我们需要停止调用。
请实现一个函数用来获取任意数量的item
const fetchListWithAmount = (amount: number = 5) { }
注意
你可以通过常规的循环来解决这个问题,也可以使用更高级的async iterators or async generators,请尽量都尝试下。
答案 普通循环
const fetchListWithAmount = async (amount = 5) => {
let res = [];
let cursor;
while (res.length < amount) {
let { items } = await fetchList(cursor);
if (items.length > 0) {
res.push(...items);
cursor = items[items.length - 1].id
} else {
break;
}
}
return res.slice(0,amount)
}
递归
const fetchListWithAmount = async (amount = 5) => {
const res = [];
function _getList(id) {
return fetchList(id).then(({ items }) => {
res.push(...items);
if (res.length >= amount || items.length == 0) {
return res.slice(0, amount);
} else {
return _getList(items[items.length-1].id);
}
})
}
return _getList();
}
评论区还有generator,async iterator,可以自己看下