【问题标题】:how to convert Async.js parallel to Bluebird如何将 Async.js 并行转换为 Bluebird
【发布时间】:2021-03-04 13:14:11
【问题描述】:

使用 async.js,我可以用处理程序定义承诺(我知道它的唯一功能),并且它给了我与不同处理程序的多态性,结果也是分离的。 我可以在蓝鸟中做到这一点吗?

async.parallel({
              cityPromises:  (cb)=>{
                  City.find({
                      areaId: {$in:locations.city}
                  }).then(result=> cb(null,result))
              },
              townPromises:  (cb)=>{
                  Town.find({
                      areaId: {$in:locations.town}
                  }).then(result=>cb(null,result))
              },
              quarterPromises:  (cb)=>{
                  Quarter.find({
                      areaId: {$in:locations.quarter}
                  }).then(result=>cb(null,result))
              }
          },(err,promises)=>{
              let {cityPromises, townPromises, quarterPromises} = promises
          })

【问题讨论】:

    标签: javascript node.js bluebird async.js


    【解决方案1】:

    经过研究,我做了这个。 http://bluebirdjs.com/docs/api/promise.props.html

    let promisesObject ={
                  cities: City
                    .find({
                        areaId: {$in:locations.city}
                    })
                    .select({ name: 1, areaId: 1})
                    .exec(),
    
                  towns:   Town
                    .find({
                        areaId: {$in:locations.town}
                    })
                    .select({ name: 1, areaId: 1})
                    .exec(),
                  quarters: Quarter
                    .find({
                        areaId: {$in:locations.quarter}
                    })
                    .select({ name: 1, areaId: 1})
                    .exec()
              }
    
    promise.props(promisesObject)
           .then((result) =>{
               let {cities, towns, quarters} = result
           });
    
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 2013-06-12
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多