Search code examples
javascriptnode.jsbluebirdasync.js

how to convert Async.js parallel to Bluebird


With async.js i can be able to define promises(i know its only functions) with handlers and its give me polymorphism with different handlers also results are seperated. Can i do this in bluebird ?

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

Solution

  • After researching i made this. 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
           });