为什么当我使用 await 和 async 修饰符时 axios 不等待响应?
Why axios doesn't wait for response when I use await with async modifier?
我在 Json format.But 中有从服务器获取数据的异步函数,它不等待响应
这就是我要说的功能
async fetchFromServer (startRow,filter, sortBy, descending,rowsPerPage) {
const response = await axios.get("${pageContext.request.contextPath}"+"/Admin/OrdersSorted",{
params: {
startRow: startRow,
filter:filter,
sortBy:sortBy,
descending:descending,
rowsPerPage:rowsPerPage
}
})
return response.data
}
我在这里调用它
onRequest(props) {
const {page, rowsPerPage, sortBy, descending} = props.pagination
const filter = Object.fromEntries(Object.entries(props.filter).filter(([_, v]) => !!v));
this.loading = true
// update rowsCount with appropriate value
// get all rows if "All" (0) is selected
// calculate starting row of data
const startRow = (page - 1) * rowsPerPage
// fetch data from "server"
const returnedData = this.fetchFromServer(startRow, filter, sortBy, descending, rowsPerPage)
alert("returndata " + Array.isArray(returnedData))
this.pagination.rowsNumber = returnedData.length
alert("length " + returnedData.length)
// clear out existing data and add new
this.data.splice(0, this.data.length, ...returnedData)
// don't forget to update local pagination object
this.pagination.page = page
this.pagination.rowsPerPage = rowsPerPage
this.pagination.sortBy = sortBy
this.pagination.descending = descending
// ...and turn of loading indicator
this.loading = false
}
你也需要在 onRequest 函数中使用 async。
喜欢
async onRequest(props) {
...
const returnedData = await this.fetchFromServer(startRow, filter, sortBy, descending, rowsPerPage)
...
}
我在 Json format.But 中有从服务器获取数据的异步函数,它不等待响应 这就是我要说的功能
async fetchFromServer (startRow,filter, sortBy, descending,rowsPerPage) {
const response = await axios.get("${pageContext.request.contextPath}"+"/Admin/OrdersSorted",{
params: {
startRow: startRow,
filter:filter,
sortBy:sortBy,
descending:descending,
rowsPerPage:rowsPerPage
}
})
return response.data
}
我在这里调用它
onRequest(props) {
const {page, rowsPerPage, sortBy, descending} = props.pagination
const filter = Object.fromEntries(Object.entries(props.filter).filter(([_, v]) => !!v));
this.loading = true
// update rowsCount with appropriate value
// get all rows if "All" (0) is selected
// calculate starting row of data
const startRow = (page - 1) * rowsPerPage
// fetch data from "server"
const returnedData = this.fetchFromServer(startRow, filter, sortBy, descending, rowsPerPage)
alert("returndata " + Array.isArray(returnedData))
this.pagination.rowsNumber = returnedData.length
alert("length " + returnedData.length)
// clear out existing data and add new
this.data.splice(0, this.data.length, ...returnedData)
// don't forget to update local pagination object
this.pagination.page = page
this.pagination.rowsPerPage = rowsPerPage
this.pagination.sortBy = sortBy
this.pagination.descending = descending
// ...and turn of loading indicator
this.loading = false
}
你也需要在 onRequest 函数中使用 async。
喜欢
async onRequest(props) {
...
const returnedData = await this.fetchFromServer(startRow, filter, sortBy, descending, rowsPerPage)
...
}