Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Promise.all在循环后执行函数_Javascript_Vue.js_Promise_Async Await - Fatal编程技术网

Javascript 使用Promise.all在循环后执行函数

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[

我正在尝试在循环结束时执行函数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[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())