I am trying to call an asynchronous function inside a promise, however the code inside the .then() is never reached, neither is the .catch(), nor is there any debug information, so I am at a loss.
If I make a call to the same asynchronous function elsewhere in the code, it works!
My suspicion is that I am incorrectly implementing bluebird promises, but I cannot see any anti-pattern in my code.. Here is some pseudocode that is basically what my code is doing:
// application entrypoint
public static ApplicationEntryPoint() {
this.MainFunction()
.then((result: any) => {
console.log(SuccessStrings[SuccessStrings.cycle_completed].toString());
this.ApplicationEntryPoint();
}).catch((error: any) => {
this.ApplicationEntryPoint();
});
}
public static MainFunction() {
return new Promise((resolve: any, reject: any) => {
// other code is here....
this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
}).then((result) => {
resolve(result);
}).catch((error) => {
reject(error);
});
});
}
public static ProblemFunction(result2) {
return new Promise((resolve: any, reject: any) => {
// other code in here....
someAsyncCall()
.then(() => {
let resultArray = this.PromisifyCalculations(result2);
return resultArray;
}).then((resultArray) => {
// do some code here
resolve(resultArray);
});
});
}
public static PromisifyCalculations(result2) {
// Call CalculationFunction() for each element
let CoinPromises: Array<any> = new Array<any>();
CoinPromises = result2.map(item => () => this.CalculationFunction(item));
return Promise.all(m_arCoinPromises.map(item => item()));
}
public static CalculationFunction() {
**// here is where the Async call DOES NOT work FOR each element..**
return new Promise((resolve: any, reject: any) => {
dataLookup(id, baseid)
.then((resultantArray) => {
// never reached
resolve(resultantArray);
}).catch((error) => {
// never reached either
reject(error);
});
});
}
Thanks!
return new Promise((resolve: any, reject: any) => {
Thats an antipattern! You wont need the Promise constructor except you wrap a callback into it. In every other case you can just return the chain:
return this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
})
Now the problem seems to be, that you never catch
the rejection of this:
someAsyncCall()
and as you wrap that in the antipattern way the error wont bubble up.