I have a http post request which returns ID. I then try to pass that ID into another function. However, inside the next function I have a timeout that will loop the function to check the status. The ID returns undefined each time inside the timeout function.
First Function
Here I have 'res' which is a result from another function. I grab the status ID from the returned json and send it to 'getAlbum'.
anotherFunction(res) {
this.getAlbum(res);
}
GetAlbum
If I do a console log immediately inside this function, it correct emits the correct ID. However, if I do it inside the 'checkAblumStatus' function, the id part is undefined.
getAlbum(id){
var statusID = id.status_id;
console.log('id = ' + statusID) // returns id
var statusIDRequest = 'url' + statusID;
var checkAblumStatus = function (statusIDRequest) {
console.log('statusIDRequest = ' + statusIDRequest) // returns undefined for the ID part
this.http.get(statusIDRequest).subscribe(res => {
if (res.status == "completed") {
// completed
} else if (res.status == "failed") {
// failed
} else {
setTimeout(checkAblumStatus, 1000);
}
});
};
setTimeout(checkAblumStatus, 1000);
}
Any help here would be very grateful :)