多次异步请求, 处理后让数据按序
talk is cheap, show the code!
const $ = require('axios')
getData()
// 处理返回的promise数组
function getData() {
const idList = [188, 187, 190, 191, 192, 193, 194, 189, 195, 196, 198]
const promiseArr = idList.map(item => {
return getDataFromServer(item)
})
Promise.all(promiseArr).then(res => {
console.log(promiseArr)
console.log(res)
})
}
// 异步数据请求
function getDataFromServer(item) {
return new Promise((resolve, reject) => {
const url = `https://xxx.xxx.xxx/data?id=${item}`
$.get(url).then(res => {
resolve(res.data)
})
})
}