2021-11-01 11:35:48 +08:00

31 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 根据页码获取学生数据返回Promise
* @param {Number} page 页码
*/
function fetchStudents(page) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < 0.3) {
reject(new Error(`网络错误!获取第${page}页数据失败!`));
return;
}
// 模拟学生数据
const stus = new Array(10).fill(null).map((d, i) => ({
id: `NO.${(page - 1) * 10 + i + 1}`,
name: `姓名${(page - 1) * 10 + i + 1}`,
}));
resolve(stus);
}, Math.floor(Math.random() * 5000));
});
}
// 利用 fetchStudents 函数,完成下面的练习
// 获取1-10页的学生最终按照页码的顺序合并成一个数组任何一页的数据获取出现错误则任务不再继续打印错误消息
// 获取1-10页的学生最终按照页码的顺序合并成一个数组如果某些页码的数据获取失败就不加入该数据即可
// 获取1-10页的学生打印最先获取到的数据如果全部都获取失败则打印所有的错误消息
// 获取1-10页的学生输出最先得到的结果有结果输出结果有错误输出错误