提问者:小点点

JavaScript:promission.all()和Async/Await和Map()


我试图理解为什么下面两个代码块会产生不同的结果。

代码块1按预期工作,并返回从数据库中查找的提供程序的数组。另一方面,代码块2返回函数数组。在理解promissione.all()和async/await时,我觉得缺少了一些简单的东西。

代码块的差异如下:

>

  • 块1:创建许诺函数数组,然后使用map运算符将其包装在异步函数中。

    块2:许诺函数的数组被创建为异步函数。因此,不调用map运算符。

    如果您不熟悉Sequelize库,那么调用的findOne()方法将返回一个promise。

    另外值得一提的是,我知道我可以使用单个find查询with和“name in”where子句来获得相同的结果,而不需要为多个select查询创建一个数组的承诺。我只是作为async/await和promission.all()的学习练习。

    代码块1:在promission.all()中使用map()

    private async createProfilePromises(profiles){
    
        let profileProviderFindPromises = [];
    
        //Build the Profile Providers Promises Array.
        profiles.forEach(profile => {
            profileProviderFindPromises.push(
                () => {
                    return BaseRoute.db.models.ProfileProvider.findOne({
                        where: {
                            name: {[BaseRoute.Op.eq]: profile.profileProvider}
                        }
                    })}
            );
        });
    
        //Map and Execute the Promises
        let providers = await Promise.all(profileProviderFindPromises.map(async (myPromise) =>{
            try{
                return await myPromise();
            }catch(err){
                return err.toString();
            }
        }));
    
        //Log the Results
        console.log(providers);
    }
    

    代码块2:在不使用map()的情况下添加异步函数

    private async createProfilePromises(profiles){
    
        let profileProviderFindPromises = [];
    
        //Build the Profile Providers Promises Array.
        profiles.forEach(profile => {
            profileProviderFindPromises.push(
                async () => {
                    try{
                        return await BaseRoute.db.models.ProfileProvider.findOne({
                            where: {
                                name: {[BaseRoute.Op.eq]: profile.profileProvider}
                            }
                        });
                    }catch(e){
                        return e.toString();
                    }
                }
            );
        });
    
        //Execute the Promises
        let providers = await Promise.all(profileProviderFindPromises);
    
        //Log the Results
        console.log(providers);
    }
    

  • 共1个答案

    匿名用户

    您的代码基本上可以归结为:

      const array = [1, 2, 3];
    
      function fn() { return 1; }
    
      array.map(fn); // [1, 1, 1]
    
      array.push(fn);
      console.log(array); // [1, 2, 3, fn]
    

    您推送一个函数(这是否async并不重要),而是希望推送调用该函数的结果:

      array.push(fn());
    

    或者在您的情况下:

     array.push((async () => { /*...*/ })());
    

    我如何编写您的代码:

     return Promise.all(profiles.map(async profile => {
       try{
         return await BaseRoute.db.models.ProfileProvider.findOne({
           where: {
             name: { [BaseRoute.Op.eq]: profile.profileProvider }
           }
         });
      } catch(e) {
        // seriously: does that make sense? :
        return e.toString();
      }
    }));