I am trying to user promises in order to do a synchronous execution.
I have followed the instructions listed in a github forum, with no luck.
getData2(user: string, guid: string) {
return new Promise<any>((resolve, reject) => {
//doing some stuff in here
resolve();
});
}
getData1(user: string, guid: string) {
return new Promise<any>((resolve, reject) => {
//doing some other stuff in here
resolve();
});
}
userCheck(user: string) {
//checking some array in here
}
userMethod(someArray: any){
var i: any;
var length = someArray.length;
for (i=0; i < length; i++){
this.getData1(someArray[i], userID).then(
this.getData2(someArray[i], userID)).then(
this.userCheck(someArray[i]));
});
})}
}
The idea is that I want to first finish getData1 and then do getData2 and then do userCheck.
When I do it the way I have laid out above I get the error message:
Argument of type 'void' is not assignable to parameter of type '(value: {}) => {} | PromiseLike<{}>'.
After fixing this issue I ran into the fact that the userCheck function is still executing before getData2 finishes. I am not sure why. the getData2 function looks like this.
getCCData(user: string, id: string){
console.log('grabbing CC information', id, user);
return new Promise((resolve, reject) =>
{
this._apiService.getAssertion(id).toPromise().then(assert =>
{
return this._apiService.getToken(assert.toString(), user);
}).then((data: any) =>
{
return this._apiService.ccMeta(data.access_token.toString(), id);
}).then(parseString(Promise)).then((information: any) =>
{
this.ccData.push(
{
key: 'userStatus',
value: information.entry
});
});
resolve();
});
}
I'm not sure if this is the correct way to design this function. Any extra insight would be great.