i am trying to break out of a for loop here on an error response and console the error message. Here is what i have tried
var error = 0;
var errorMsg;
for (let i = 0; i < this.fileArray.length; i++) {
if ( this.fileArray[i] != undefined ) {
if ( error == 0 ) {
const fd = new FormData()
fd.append('file', this.fileArray[i], this.fileArray[i].name)
this.data.ImgUploader(fd).subscribe(
res => {
if ( res['code'] != '00' ) { //error occurred. 1 is assigned to var error
error = 1
errorMsg = res['message']
} else {
error = 0
}
}
)
} else {
console.log(errorMsg)
break;
}
}
}
I am sending some image files via the ImgUploader function and recieving the response via the callback. I am trying to watch for errors via the error codes and assign 1 to the variable error. I expect variable error == 1 now but the code still enters into the code block where error == 0. Please where could i be going wrong. Thanks.