我正在编写JavaScript函数来处理map方法中的api调用,在map方法中完成所有任务之前我的函数执行错误的结果。但它不符合我的期望。
我的代码:
function vehicleAndWorker(hook){
var routeArray = [];
return new Promise(function(resolve, reject) {
hook.data.map(d => {
var routeArray = [];
delete d.driverId
delete d.vehicle
delete d.workers
return hook.app.service('vehicles')
.find({ query: {vehicleId: d.vehicleId}})
.then(result=>{
var vehicleTypeId = result.data[0].vehicleTypeId;
return hook.app.service('vehicle-types')
.find({ query: {vehicleTypeId: vehicleTypeId }})
.then(result=>{
d.vehicle = {
vehicleId: d.vehicleId,
vehicleTypeId: vehicleTypeId,
fixedCost: result.data[0].fixedCost,
variableCost: result.data[0].variableCost
}
return hook.app.service('workers')
.find({ query: {assignedVehicleId: d.vehicleId}})
.then(result=>{
d.workers = [{
id:result.data[0].workerId,
name:result.data[0].name,
type:result.data[0].type
}];
delete d.vehicleId;
})
})
})
})
resolve(hook)
})
}
在执行我的地图作业之前,返回执行如何解决此问题。
首先链接你的承诺,而不是嵌套它们。如果你使用嵌套,那么你不需要承诺。
第二次使用承诺所有
function vehicleAndWorker(hook){
var all = [];
var i =0;
hook.data.map(d => {
delete d.driverId
delete d.vehicle
delete d.workers
all[i] = hook.app.service('vehicles')
.find({ query: {vehicleId: d.vehicleId}})
.then(result=>{
var vehicleTypeId = result.data[0].vehicleTypeId;
return hook.app.service('vehicle-types').find({ query: {vehicleTypeId: vehicleTypeId }})
})
.then(result=>{
d.vehicle = {
vehicleId: d.vehicleId,
vehicleTypeId: vehicleTypeId,
fixedCost: result.data[0].fixedCost,
variableCost: result.data[0].variableCost
}
return hook.app.service('workers').find({ query: {assignedVehicleId: d.vehicleId}})
})
.then(result=>{
d.workers = [{
id:result.data[0].workerId,
name:result.data[0].name,
type:result.data[0].type
}];
delete d.vehicleId;
return result
})
i++;
})
return Promise.all(all).then(hook);
}
尝试使用Promise.all()
您的“.map”调用返回一组Promises。所以它立即返回,
function vehicleAndWorker(hook){
var routeArray = [];
return new Promise(function(resolve, reject) =>
Promise.all(hook.data.map(d => {....})
.then(() => resolve(hook))
);
}
正如@Akshay所提到的,我们甚至可以减少这个功能代码,并在其他方面移动你的“map”功能,所以你的vahicle将是单行:
var vehicleAndWorker = (hook) => Promise.all(hook.data.map(d => your_map_function)
.then(() => hook);
试试这个
function vehicleAndWorker(hook){
var routeArray = [];
return new Promise(function(resolve, reject) {
hook.data.map(d => {
var routeArray = [];
delete d.driverId
delete d.vehicle
delete d.workers
hook.app.service('vehicles')
.find({ query: {vehicleId: d.vehicleId}})
.then(result=>{
var vehicleTypeId = result.data[0].vehicleTypeId;
hook.app.service('vehicle-types')
.find({ query: {vehicleTypeId: vehicleTypeId }})
.then(result=>{
d.vehicle = {
vehicleId: d.vehicleId,
vehicleTypeId: vehicleTypeId,
fixedCost: result.data[0].fixedCost,
variableCost: result.data[0].variableCost
}
hook.app.service('workers')
.find({ query: {assignedVehicleId: d.vehicleId}})
.then(result=>{
d.workers = [{
id:result.data[0].workerId,
name:result.data[0].name,
type:result.data[0].type
}];
delete d.vehicleId;
})
})
})
resolve(hook)
})
})
}