簡體   English   中英

為什么 axios 在我使用帶有 async 修飾符的 await 時不等待響應?

[英]Why axios doesn't wait for response when I use await with async modifier?

我有異步 function 以 Json 格式從服務器獲取數據。但它不等待響應這是 function 我在說

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 function 中使用異步。

喜歡

async onRequest(props) {
                ...
                const returnedData = await this.fetchFromServer(startRow, filter, sortBy, descending, rowsPerPage)
                ...
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM