需求
直接上代码, 如有不正确请大神指点,注释就不想写了
```javascript
let arr = []
const fetchData = () => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
return new Promise(resolve => {
setTimeout(function () {
resolve(data)
}, 150)
})
}
const getData = function (total) {
function fn() {
return new Promise((resolve, reject) => {
fetchData().then(res => {
resolve(res)
})
}).then(data => {
arr = [...arr, ...data]
// console.log(arr);
if (arr.length >= total) {
console.log(333);
return new Promise((resolve, reject1) => {
return resolve(arr)
})
} else {
return fn()
}
})
}
return fn()
}
getData(100).then(res => {
console.log(1);
console.log(res);
})