我想在其他3个数组的帮助下构造一个数组。我为此目的使用JS承诺但是无法正确实现。 所以我基本上想要做的是先填充一个数组。
let getPlacePromise = function() {
return new Promise(function(resolve,reject){
this.getplaceArray= this.getfilterPlaces(inputdata);
resolve("got places\n" );
});
};
let getTransporterPromise= function(message) {
return new Promise(function(resolve,reject){
this.getTransporterArray= this.getfilterTransporter(inputdata);
resolve(message+"got Transporter");
});
};
let getVehiclePromise = function(message) {
return new Promise(function(resolve,reject){
this.getVehicleArray= this.getfilterVehicles(inputdata);
resolve(message+"got vehicle");
});
};
getPlacePromise().then(function(result){
return getTransporterPromise(result);
}).then(function(result){
return getVehiclePromise(result);
}).then(function(result){
this.AllDropdownValues= this.getTransporterArray.concat(this.getVehicleArray);
});
我知道这段代码应该有错误。我无法提出正确的实施方案。我希望 getplaceArray 首先填充,而不是仅应完成其他其他数组。是否可以在promises中调用函数。我在这里遇到的错误是 getplaceArray,getTransporterArray,getVehicleArray 未定义。
getfilterTransporter函数:
getfilterTransporter(autocompleteInputData) {
var k= this.checkRegex(autocompleteInputData);
this.getfilteredTransporter= this.filterTransporters(k);
return this.formatTransporterValue(this.getfilteredTransporter);
}
getfilterVehicle:
getfilterVehicles(autocompleteInputData) {
var k= this.checkRegex(autocompleteInputData);
this.getfilteredVehicle= this.filterVehicles(k);
return this.formatVehicleValue(this.getfilteredVehicle);
}
getfilterPlace
getfilterPlaces(autocompleteInputData) {
if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object')
return null;
this.placeData.getPlacesFromPig(autocompleteInputData)
.subscribe(response => {
return this.formatPigResponse(response);
});
}
修改:
稍微修改了答案。休息正在运行但是 this.getfilterPlaces ,其中有一个在其中运行的订阅方法来填充数据不起作用。
let a=[], b=[],c=[];
a= this.getfilterPlaces(inputdata);
b= this.getfilterTransporter(inputdata);
c= this.getfilterVehicles(inputdata);
let getplaceArray = [],
getTransporterArray = [],
getVehicleArray = [];
let getPlacePromise = function () {
const self = this;
return new Promise(function (resolve, reject) {
getplaceArray = a;
resolve("got places\n");
});
};
let getTransporterPromise = function (message) {
const self = this;
return new Promise(function (resolve, reject) {
getTransporterArray = b
resolve(message + "got Transporter");
});
};
let getVehiclePromise = function (message) {
const self = this;
return new Promise(function (resolve, reject) {
getVehicleArray = c
resolve(message + "got vehicle");
});
};
getPlacePromise().then(function (result) {
return getTransporterPromise(result);
}).then(function (result) {
return getVehiclePromise(result);
}).then(function (result) {
var AllDropdownValues = getTransporterArray.concat(getVehicleArray).concat(getplaceArray);
console.log(AllDropdownValues);
});
答案是 使用async和await.Below是代码。
async function getfilterPlaces(autocompleteInputData) {
if (autocompleteInputData == '' || typeof(autocompleteInputData) == 'object')
return null;
return this.placeData.getPlacesFromPig(autocompleteInputData)
.toPromise()
.then(response => this.formatPigResponse(response));
}
调用函数filterAllComponent(inputdata)时:
async function filterAllComponent(inputdata) {
let a=[], b=[], c=[];
a = await this.getfilterPlaces(inputdata);
b = this.getfilterTransporter(inputdata);
c = this.getfilterVehicles(inputdata);
[...]
答案 0 :(得分:0)
试试这个
let getplaceArray = [],
getTransporterArray = [],
getVehicleArray = [];
let getPlacePromise = function () {
const self = this;
return new Promise(function (resolve, reject) {
getplaceArray = self.getfilterPlaces(inputdata);
resolve("got places\n");
});
};
let getTransporterPromise = function (message) {
const self = this;
return new Promise(function (resolve, reject) {
getTransporterArray = self.getfilterTransporter(inputdata);
resolve(message + "got Transporter");
});
};
let getVehiclePromise = function (message) {
const self = this;
return new Promise(function (resolve, reject) {
getVehicleArray = self.getfilterVehicles(inputdata);
resolve(message + "got vehicle");
});
};
getPlacePromise().then(function (result) {
return getTransporterPromise(result);
}).then(function (result) {
return getVehiclePromise(result);
}).then(function (result) {
this.AllDropdownValues = getTransporterArray.concat(getVehicleArray);
});