如何在for循环中运行异步功能

我正在尝试运行一个异步功能,该功能将发布请求发送到服务器,并将响应记录在控制台中。每当我运行for循环时,都不会发生任何事情。我在stackoverflow上检查了一个不同的类似问题,但在我的情况下没有任何效果。

请注意,此代码适用于android的ionic + angular。

下面是该函数的代码。

postAPI(sufixUrl, queryString, data, header: any = {}) {
    // console.log(sufixUrl + "\n=>", queryString + "\n=>", data + "\n=>", header);
    this.isDisabled = true;

    data = (header.is == 'yes') ? JSON.parse(data) : data;

    let type = this.fileNameSubStringFn(sufixUrl, '?');
    return new Promise(resolve => {
        this.http.post(this.con.baseUrl + sufixUrl + queryString, data)
          .subscribe(res => {
            // res = res.json();
            // res = JSON.parse(res.data);
            console.log("RES:", res);
            this.hideLoader();
            resolve(this.data);
            return;
          }, (err) => {
            this.logFn("ERROR:" + JSON.stringify(err));
            this.isDisabled = false;
            // this.isConnected = false;
          });
    });

  }

下面是按顺序访问上述功能以提交数据的代码。

var i;
            for (i = 0; i < summeriesArray.length; i++) {
              this.postAPI('service_summery', '', JSON.stringify(summeriesArray[i]), { is: 'yes', method: 'post' }).then(data => {
                this.hideLoader();
                this.toast("Successfully saved");
                console.log(data);

              });
            }

code inside this.postAPI is not executing which means the data is not being posted and I'm getting no errors so I don't know what is going on here.

I'm expecting the this.postAPI to run successfully in a for lop. Thank you, posted with Love.

评论