Javascript 等待而不是等待

Javascript 等待而不是等待,javascript,async-await,Javascript,Async Await,我有以下代码: var arrayAxios=[]; 常量数组=异步()=>{ 等待睡眠(3000); _.forEach(任务,任务=>{ 让res=u0.includes(“selected”,task.value); 如果(res){ 这是axios .get(url,{params:{data1}}) 。然后(响应=>{ push(this.axios.post(url,{dataAAA})); }); } }); }; var promissions=async()=>{ const

我有以下代码:

var arrayAxios=[];
常量数组=异步()=>{
等待睡眠(3000);
_.forEach(任务,任务=>{
让res=u0.includes(“selected”,task.value);
如果(res){
这是axios
.get(url,{params:{data1}})
。然后(响应=>{
push(this.axios.post(url,{dataAAA}));
});
}
});
};
var promissions=async()=>{
const arrayAxios=等待数组();
等待承诺。全部(arrayAxios)
。然后(响应=>{
重置所有_dataAAA();//{
console.log(错误);
});
};
承诺();
您正在寻找的

async function array() {
  await sleep(3000);
  var arrayAxios = []; // declare the array locally
  _.forEach(tasks, task => {
    let res = _.includes("selected", task.value);
    if (res) {
      arrayAxios.push(this.axios
//    ^^^^^^^^^^^^^^^ push to the array immediately, not asynchronously
        .get(url, { params: { data1 } })
        .then(response => {
          return this.axios.post(url, { dataAAA }));
//        ^^^^^^ chain the promises
        })
      );
    }
  });
  return arrayAxios;
//^^^^^^ give the result to the caller
}

我简化了您的示例,希望您能够了解发生的情况:)

您需要从一个数组返回一个承诺数组,才能使用Promise.all解析它们

使用
wait Promise.all()没有意义。然后使用
wait
then

数组从未返回请求承诺,因此在等待时,它可能会立即得到解决

我不相信睡眠也是node中的一个函数

任何问题都让我知道,我很乐意帮忙

const axios={
获取:异步(任务)=>{
returntask.res;
},
post:async(任务)=>{
返回任务;
}
}
const tasks=[{url:'get',res:{get:'get the thing',post:'thing to post'},{url:'get 2',res:{get:'get the thing 2',post:'thing to post 2'}]
const getRequest=async(url='')=>{
const res=等待axios.get(url)
console.log(res.got)
返回postRequest(res.post)
}
const postRequest=async(url='')=>{
const res=wait axios.post(url)
console.log(res)
返回res;
}
常量数组=异步()=>{
const createRequests=异步任务=>{
返回getRequest(任务)
}
返回tasks.map(createRequests).filter(布尔值);
};
const promises=async()=>{
console.log('waiting')
const res=等待数组()
const all=等待承诺。all(res)
console.log('等待')
console.log('可以调用您的重置')
};

promises();
睡眠(3000)
调用的意义是什么?
arrayAxios
(promises中的那一个)是
未定义的,因为
数组
不返回任何内容您没有返回承诺,所以它不会等待