I am trying to properly execute some asynchronous calls. I basically have something that looks like this in order to getData1
, then getData2
, and finally userCheck
:
for (const item of someArray) {
this.getData1(item, userID).then(() =>
this.getData2(item, userID).then(() =>
this.userCheck(item)
)
);
}
and then inside getData1
and getData2
I subscribe to a GET
request in a service I have created. I have one of the functions working properly because it is a just a simple Get request. I am having trouble with the other function. It is basically three GET
request where the second call relies on the first and the third call relies on the second. I have incorporated promises and then
statements to try and achieve this but it is still not working. The one I do not working I have tried several different things but have had no luck. My function currently looks like this.
getData2(user: string, id: string)
{
return new Promise((resolve, reject) =>
{
var status: string;
this._apiService.getAssertion(id).toPromise().then((data: any) =>
{
let assert = data.toString();
console.log(assert);
this._apiService.getToken(assert, user).toPromise().then((data: any) =>
{
let tkn = data.access_token.toString();
console.log(tkn);
this._apiService.ccMeta(tkn, guid).toPromise().then((information: any) =>
{
parseString(information, (err, result) =>
{
if (err)
{
throw new Error(err);
}
else
{
status = result['entry']
this.ccData.push(
{
key: 'userStatus',
value: status
})
}
});
});
});
}).then(() =>
resolve()).then();
})