使用promises和http调用在Angular中循环

时间:2017-03-07 17:36:12

标签: arrays angular for-loop promise httprequest

我的目标是通过for循环填充数组。然后在HTTP POST调用

中发送该数组

即:首先上传所有文件,然后按数组中的每个文件详细信息。

// For multiple files
for (let i = 0; i < projectRefFilesForUploading.length; i++) {
    this.postSingleFile([] = projectRefFilesForUploading[i].file, projectDir).then(uploadedFiles => {

        let filePath = uploadedFiles['projectRefFiles'][0].fd;
        filePath = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
        let filename = uploadedFiles['projectRefFiles'][0].filename;
        let size = uploadedFiles['projectRefFiles'][0].size;
        let type = uploadedFiles['projectRefFiles'][0].type;
        let status = uploadedFiles['projectRefFiles'][0].status; // finished
        let fileNote = projectRefFilesForUploading[i].fileNote;

        projectRefFilesDataArray.push({fileName: filename, filePath: filePath, size: size, type: type, fileNote: fileNote});
    });   
}

即:现在在http post post中发布数组。

let body = {
    projectId: projectId,
    projectRefFiles: projectRefFilesDataArray
}

let headers = new Headers();
let options: RequestOptionsArgs = { headers: headers, withCredentials: true }
return new Promise((resolve, reject) => {

    this.http.post( this.apiEndpoint + "/add/all", body, options).toPromise()
    .then(response => {
        let jsonData = response.json();
        if (jsonData.apiStatus == 1) {
            resolve(jsonData);
        }
        else reject(jsonData.message);
    })
    .catch(reason => reject(reason.statusText));
});

问题是由于async函数调用,http调用将其数据并行发布到循环执行。所以它在http调用中发送一个空数组。如果我把调用方法放在循环中然后它工作。但它创建了太多http调用以及循环。

请帮助我如何检测,循环完成后执行http调用

1 个答案:

答案 0 :(得分:0)

试试这个:

let promise = new Promise((resolve, reject) => {
        let count = 0;
        for (let i = 0; i < projectRefFilesForUploading.length; i++) {
            this.postSingleFile([] = projectRefFilesForUploading[i].file, projectDir).then(uploadedFiles => {
                count++;
                let filePath = uploadedFiles['projectRefFiles'][0].fd;
                filePath = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
                let filename = uploadedFiles['projectRefFiles'][0].filename;
                let size = uploadedFiles['projectRefFiles'][0].size;
                let type = uploadedFiles['projectRefFiles'][0].type;
                let status = uploadedFiles['projectRefFiles'][0].status; // finished
                let fileNote = projectRefFilesForUploading[i].fileNote;

                projectRefFilesDataArray.push({fileName: filename, filePath: filePath, size: size, type: type, fileNote: fileNote});
               if(count === projectRefFilesForUploading.length){
                  resolve(projectRefFilesForUploading);
               }
            });   
        }
        });


    promise.then(function(projectRefFilesDataArray){
    let body = {
        projectId: projectId,
        projectRefFiles: projectRefFilesDataArray
    }

    let headers = new Headers();
    let options: RequestOptionsArgs = { headers: headers, withCredentials: true }
    return new Promise((resolve, reject) => {

        this.http.post( this.apiEndpoint + "/add/all", body, options).toPromise()
        .then(response => {
            let jsonData = response.json();
            if (jsonData.apiStatus == 1) {
                resolve(jsonData);
            }
            else reject(jsonData.message);
        })
        .catch(reason => reject(reason.statusText));
    });

    });