如何将 Async.js 并行转换为 Bluebird
how to convert Async.js parallel to Bluebird
有了 async.js,我可以用处理程序定义承诺(我知道它的唯一功能),它为我提供了不同处理程序的多态性,结果也是 分离的 。
我可以在 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
})
经过研究我做了这个。
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
});
有了 async.js,我可以用处理程序定义承诺(我知道它的唯一功能),它为我提供了不同处理程序的多态性,结果也是 分离的 。 我可以在 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
})
经过研究我做了这个。 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
});