1

I have a frustrating issue and I cannot find why it is happening

I have a function as follows:

  public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){

    some codes .....
    return new Promise((resolve, reject) => {
      this._resultsDatastore.bulkInsert(
        databaseName,
        objects
      ).then(succ => {
        // I can see succ is printed successfully in console
        console.log(succ);
        // when I resolve it here I assume I am returning the result
        resolve(succ)
      }, err => {
        console.log(err);
        reject(err)
      })
    })
  }

So as you can see I resolve(succ) and this way I am returning succ to the caller function

Now I have:

 public async loadResults(
    searchId: string,
    authorization: string
  ) {

   some code....
    const results = udsQuery.pages.map(async (pageNumber: number) => await this.PageResult(searchRecord,authorization,pageNumber,databaseName))


    let all: any=[];
    await Promise.all(results).catch(e => {
      console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
    }).then(res=>{
      console.log("suuuuuuuuuuuuuuucesssssssssssssss");
      // issue is here: below prints undefined though succ has value 
      console.log(res);
      all.push(res);
      return res;
    });

  }

So now that I call the PageResult in a map and use promis.all to reslove it instead of succ that I returned in PageResult I get undefined Please see this section in above code:

  console.log("suuuuuuuuuuuuuuucesssssssssssssss");
  // issue is here: below prints undefined though succ has value 
  console.log(res);

Am I missing anything?

Learner
  • 1,686
  • 4
  • 19
  • 38
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jan 09 '20 at 15:16
  • You will get `undefined` when there was a rejection and the `.catch(e => { console.log("…ERROR…"); })` ran – Bergi Jan 09 '20 at 15:18
  • Also please show us your exact code. Are the `return` statements nested in anything? – Bergi Jan 09 '20 at 15:19
  • @Bergi no even in success I see it is undefined – Learner Jan 09 '20 at 15:19
  • @Bergi Thanks for the answer but there is only one return statement and even before reaching that my issue exists – Learner Jan 09 '20 at 15:21
  • @Bergi when you say antipattern do you mean I should not use async await and use nested then instead? – Learner Jan 09 '20 at 15:22
  • 1
    No, I'm saying that you should not use `new Promise(…)` anywhere in that function. – Bergi Jan 09 '20 at 15:23

1 Answers1

0

Somehow I did not like the way you have written the code, I tried to modiy that code first. Also another mistake I can see, you are executing your PageResult() function before hand instead it should be executed by Promise.all block.

I have not tested the code but this is how it should be:

 public async PageResult(searchRecord:Search,authorization:string,pageNumber:number,databaseName:string){

        try
        {
            const succ =  await this._resultsDatastore.bulkInsert(
                databaseName,
                objects);
            return succ;
        }
        catch (e)
        {
            throw e;
        }
    }


    public loadResults(searchId: string, authorization: string )
    {   
        const all: any=[];
        const pormises: any=[];
        const results = udsQuery.pages.map((pageNumber: number) =>
            pormises.push(this.PageResult(searchRecord, authorization, pageNumber, databaseName)));

        return new Promise((resolve, reject) =>
        {
            Promise.all(results)
                .then(res=>
                {
                    console.log("suuuuuuuuuuuuuuucesssssssssssssss");
                    // issue is here: below prints undefined though succ has value

                    console.log(res);
                    all.push(res);

                    return resolve(all);
                })
            .catch(e =>
            {
                console.log("^^^^^^^^^^^^^^^^^^^^^EEEERRRRRROR^^^^^^^^^^^^^^^^^^^^^^");
            });
        });

    }
Sohan
  • 6,252
  • 5
  • 35
  • 56