Javascript 使用Promise.all在循环后执行函数
我正在尝试在循环结束时执行函数exportList()。 但函数不会在循环结束时执行。 执行函数exportList()时,数组this.exportList未满Javascript 使用Promise.all在循环后执行函数,javascript,vue.js,promise,async-await,Javascript,Vue.js,Promise,Async Await,我正在尝试在循环结束时执行函数exportList()。 但函数不会在循环结束时执行。 执行函数exportList()时,数组this.exportList未满 async getUser() { let promsArr = []; for (let i = 0; i < this.User.length; i++) { let requestUri = `domain/GetPropertiesFor(accountName=@v)?@v='${this.User[
async getUser() {
let promsArr = [];
for (let i = 0; i < this.User.length; i++) {
let requestUri = `domain/GetPropertiesFor(accountName=@v)?@v='${this.User[i].login}'`;
let prom = axios
.get(requestUri, {
headers: {
Accept: "application/json;odata=verbose"
}
})
.then(response => {
let props = {};
let d = response.data.d;
let department, email = "";
if (d.UserProfileProperties.results.length > 0) {
for (var i = 0; i < d.UserProfileProperties.results.length; i++) {
if (d.UserProfileProperties.results[i].Key === "Department") {
department = d.UserProfileProperties.results[i].Value;
}
}
}
props = {
dep: department,
email: d.Email
};
promsArr.push(prom);
this.ExportList.push(props);
})
.catch(error => {
console.log(error);
});
}
await Promise.all([promsArr]).then(this.exportList())
}
async getUser(){
让Promsar=[];
for(设i=0;i{
设props={};
设d=response.data.d;
让部门,电子邮件=”;
如果(d.UserProfileProperties.results.length>0){
对于(var i=0;i{
console.log(错误);
});
}
等待承诺。所有([Promsar])。然后(this.exportList())
}
我会避免在同一个函数中编写async/await和promise函数。。。正如你所看到的,这让人有点困惑
在本例中,我将其重写(未测试)作为示例,说明如何将承诺生成(用于并行处理)与异步/等待语法分离
async getUser() {
const processResponse = function(response){
let props = {};
let d = response.data.d;
let department, email = "";
if (d.UserProfileProperties.results.length > 0) {
for (var i = 0; i < d.UserProfileProperties.results.length; i++) {
if (d.UserProfileProperties.results[i].Key === "Department") {
department = d.UserProfileProperties.results[i].Value;
}
}
}
props = {
dep: department,
email: d.Email
};
return props;
};
const getAccountProps = function(login){ // this function returns a promise!
let requestUri = `domain/GetPropertiesFor(accountName=@v)?@v='${login}'`;
return axios
.get(requestUri, {
headers: {
Accept: "application/json;odata=verbose"
}
})
.then(processResponse);
};
try{
let promsArr= this.User.map(u=>getAccountProps(u.login));
let propsArr = await Promise.all(promsArr);
this.ExportList.push(propsArr);
await this.exportList();
}catch(e){
console.log('An error occurred while getting all account properties.');
}
}
async getUser(){
const processResponse=函数(响应){
设props={};
设d=response.data.d;
让部门,电子邮件=”;
如果(d.UserProfileProperties.results.length>0){
对于(var i=0;igetAccountProps(u.login));
让propsArr=等待承诺。全部(promsArr);
this.ExportList.push(propsArr);
等待此消息。exportList();
}捕获(e){
log('获取所有帐户属性时出错');
}
}
首先,您传递的promArr错误Promise.all([promsar])
是一个数组数组。。。但是第二,promArr.push
是异步执行的-因此当代码达到Promise时数组将为空。所有的第二个问题是。然后
需要一个函数-您的代码正在传递调用函数的结果
就在for循环结束之前,由于getUser没有返回任何内容,因此不需要async
或wait
。。。所以最后3行就是promArr.push(prom);}Promise.all(promsArr)。然后(()=>this.exportList())