【问题标题】:Why axios doesn't wait for response when I use await with async modifier?当我将 await 与 async 修饰符一起使用时,为什么 axios 不等待响应?
【发布时间】:2021-05-21 20:42:07
【问题描述】:

我有异步功能,可以从服务器以 Json 格式获取数据。但它不等待响应 这就是我说的功能

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
            }

【问题讨论】:

  • 一个异步函数返回一个承诺
  • 感谢您的信息

标签: javascript async-await axios request async.js


【解决方案1】:

你还需要在 onRequest 函数中使用异步。

喜欢

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

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多