Vueresource等待上一个http请求完成,然后执行下一个请求

时间:2018-09-06 13:43:38

标签: vuejs2 vue-resource

我正在尝试加载大型数据集并通过以较小的时间间隔获取数据来对其进行细分

那就是

假设我有1000条记录,并且我希望以10的步长获取数据,但是仅在当前请求完成后才请求下一个10

所以目前这是在做什么,但是它发出多个http请求,我不知道何时完成一个请求

  processData(){
    this.fetchedtrucks = 0;
    this.dataLoadingCmplete = false;
    this.exportstart = true;
    this.ttatdatatrucks = [];
    let i = 0;
    do {
        const page = (i == 0) ? 0 : (i / (this.paginatorval));
        //send http request

          this.$http.get("v1/data-trucks",
                {
                    from: this.duration_from,
                    to: this.duration_to,
                    page: page,
                    pagination: this.paginatorval,
                    filters: this.filters,
                    only_completed: this.only_completed,
                    non_reg: this.non_reg
                }
            ).then(
                res => {
                    this.ttatdatatrucks = this.ttatdatatrucks.concat(this.getTransformedData(res.ttatdata));
                    this.fetchedtrucks += this.paginatorval;
                    if (this.fetchedtrucks >= this.totalRecords) {
                        this.dataLOadingCompleted();
                    }


                }, err => {
                    this.fetchedtrucks += this.paginatorval;
                    this.errorloadingdata = true;
                }
            )

        i += this.paginatorval;

    } while (i < this.totalRecords);

以上方法有效,但并不是很整洁,因为当我检查浏览器开发工具时,我可以看到发出了100多个http请求,期望做的是仅在当前请求完成后才发出下一个http请求。

我该如何实现?

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是因为您的do..while循环一次启动了所有http请求。它与您的承诺无关,因为它是在外部定义的。

在不重构大量代码的情况下,处理此问题的最简单方法是使用JavaScript异步/等待关键字。我将演示该解决方案,但是如果您无法使用这些运算符,那么我将概述如何重构代码以获得所需的内容。

首先简单的方法: 您需要使processData异步,以便可以在其中放入一个await操作符。为此,您可以在processData声明中添加异步关键字-就像这样:

async processData() {...

接下来,您将需要删除.then包装器,而应使用await运算符。您在do..while中的代码将如下所示:

     try {
            const res = await this.$http.get("v1/data-trucks",
                {
                    from: this.duration_from,
                    to: this.duration_to,
                    page: page,
                    pagination: this.paginatorval,
                    filters: this.filters,
                    only_completed: this.only_completed,
                    non_reg: this.non_reg
                }
            )
            this.ttatdatatrucks = 
            this.ttatdatatrucks.concat(this.getTransformedData(res.ttatdata));
            this.fetchedtrucks += this.paginatorval;
            if (this.fetchedtrucks >= this.totalRecords) {
                this.dataLOadingCompleted();                    
     } catch (err) {
             this.fetchedtrucks += this.paginatorval;
             this.errorloadingdata = true;
     }

此代码将使您的while循环需要很长时间才能运行,但是它将满足您等待每个http请求完成以执行下一个请求的要求。

现在,如果await运算符不可用,那么您将需要取消do ... while循环。我认为您无法轻松完成这项工作。我将概述您需要做什么。

1)删除do ... while循环

2)将i变量和任何其他适当的变量添加为processData循环的参数

3)从http请求的.then块中递归调用processData()

4)确保在函数中添加适当的逻辑以退出递归

看起来有点像这样:

processData(i, complete) {
 if (i >= complete) return;
 this.$http.get()...
 .then(res => {
    //do processing and stuff
    i++
    processData(i, complete)
})

这不是真正的递归,我想-这只是做的……虽然伪装成递归,但是它将实现您追求的结果,即同步的HTTP请求