0

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!

James Tucker
  • 15
  • 1
  • 6

1 Answers1

0
  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.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I forgot to mention that there is a bootstrap method which calls the application entry point (because I intend to loop the application multiple times). So you're saying on MainFunction() there is no need for the promise constructor (return new Promise)? – James Tucker Mar 17 '18 at 12:29
  • @james tucker there is no need to use a promise constructor in your whole code. – Jonas Wilms Mar 17 '18 at 12:32
  • Thanks dude you are a life saver, you made me go back into the docs and understand why this is an anti-pattern! https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – James Tucker Mar 17 '18 at 16:02
  • @james glad to help :) – Jonas Wilms Mar 17 '18 at 18:36