已解决的承诺虽然有价值,但返回的结果不确定

我遇到一个令人沮丧的问题,我找不到原因

我的功能如下:

  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)
      })
    })
  }

因此您可以看到我resolve(succ),这样我就将succ返回给调用者函数

现在我有:

 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;
    });

  }

因此,现在我在地图中调用PageResult并使用promis.all重新修改它,而不是在PageResult中返回的succ我得到了未定义的信息,请参见上面的代码:

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

我想念什么吗?

lipanhaoran 回答:已解决的承诺虽然有价值,但返回的结果不确定

以某种方式我不喜欢您编写代码的方式,我尝试首先修改该代码。我还能看到的另一个错误是,您正在手动执行PageResult()函数,而应该由Promise.all块执行。

我尚未测试代码,但这应该是这样:

 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^^^^^^^^^^^^^^^^^^^^^^");
            });
        });

    }
本文链接:https://www.f2er.com/2796677.html

大家都在问