Skip to content Skip to sidebar Skip to footer

Pass Data To Next Request In For Loop

I have a image upload Request with a param. I want to send response of 1st request to all next requests inside for loop. for (let i = 0; i < e.target.files.length; i++) { this

Solution 1:

The following snippet will perform every upload in series, updating the this.postId value each time. Using arrow functions this context is preserved.

(If your this.http.uploadImages method already returns a promise you don't need to instantiate a new one)

const upload = async () => {
    for (let i = 0; i < e.target.files.length; i++) {
        awaitnewPromise((resolve, reject) => {
            this.http
                .uploadImages(e.target.files[i], 'UploadImage', this.postId)
                .subscribe((res: any) => {
                    if (res.status) {
                        this.postId = res.data.post_id;
                        this.userImages.push({img: res.data});

                        resolve();
                    } else {
                        reject();
                    }
                });
        });
    }
};

Solution 2:

Post a Comment for "Pass Data To Next Request In For Loop"