JavaScript promise.all axios request not working correctly.

huangapple go评论55阅读模式
英文:

Javascript promise.all axios request not working correctly

问题

我正在尝试使用以下代码进行多个axios GET调用:

console.log("getting here");
const promises = Data?.stuff.map(async (id: any) => {
  axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
    .then(response => {
      console.log(response.data);
      return response.data;
    });
  const tempLesson: any = await Promise.all(promises);
  console.log("never getting here");
});

但它直到最后才执行。问题是什么?

英文:

I am trying to make a bunch of axios get calls with this code:

console.log("getting here")
const promises = Data?.stuff.map(async(id: any) => {
      axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
        .then(response => {
          console.log(response.data)
          return response.data;
        });
      const tempLesson: any = await Promise.all(promises);
      console.log("never getting here")

but it does not execute until the end. Whats the problem?

答案1

得分: 0

Your code are missing return statement and closing brackets

Your previous code should not compile. I don't know how the first console.log can run.

console.log("getting here")
const promises = Data?.stuff.map((id: any) => { // Removed async
    // Missing return statement
    return axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
        .then(response => {
            console.log(response.data)
            
            return response.data;
        })
}) // Missing }) here

const tempLesson: any = await Promise.all(promises);
console.log("getting here")
英文:

Your code are missing return statement and closing brackets

Your previous code should not compile. I don't know how the first console.log can run.

console.log("getting here")
const promises = Data?.stuff.map((id: any) => { // Removed async
    // Missing return statement
    return axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
        .then(response => {
            console.log(response.data)
            
            return response.data;
        })
}) // Missing }) here

const tempLesson: any = await Promise.all(promises);
console.log("getting here")

答案2

得分: 0

Promise.all(promises) 应该在 .map() 函数的外部等待。Promise.all() 的作用是接受一个包含多个 promise 的数组(在你的情况下是 Axios 请求的数组),只有当数组中的所有 promise 都解决或有一个被拒绝时才会解决。

所以,如果你想这样做:

const promises = Data?.stuff.map((id: any) => {
    return axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
        .then(response => {
            console.log(response.data);
            return response.data;
        });
});

Promise.all(promises)
    .then((results) => {
        console.log("所有 promises 都已解决");
        console.log(results);
    })
    .catch((error) => {
        console.log("一个 promise 被拒绝并出现错误", error);
    });

就是这样。

英文:

The Promise.all(promises) should be awaited outside of the .map() function. What Promise.all() does is it takes an array of promises (in your case, an array of Axios requests) and only resolves when all the promises in the array have resolved, or one has rejected.

so, if you want to do it:

const promises = Data?.stuff.map((id: any) => {
    return axios.get('http://127.0.0.1:8080/api/v1/stuff/' + id)
        .then(response => {
          console.log(response.data);
          return response.data;
        });
});
    
Promise.all(promises)
    .then((results) => {
        console.log("All promises resolved");
        console.log(results);
    })
    .catch((error) => {
        console.log("A promise rejected with error", error);
    });

That's all.

huangapple
  • 本文由 发表于 2023年7月10日 15:06:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定