Alwaysblue
Alwaysblue

Reputation: 11830

TypeError: #<Promise> is not iterable

I was trying to use promise.all to do multiple api calls but for some reason it it throwing the following error

TypeError: # is not iterable

My Promise is fairly simple (this is probably the second time I am using Promise.all)

componentWillMount() {
    const id = this.props.location.pathname
    let axiosHome = axios.get('/home')
    let axiosUserData = axios.get("/profile/" + id)
    Promise.all(axiosHome,  axiosUserData).then(response => {
        console.log(response)
    }).catch(error => {
        console.log(error)
    })
  }

Question: Any idea why I could be getting this error? Also, can someone also explain when is that we use resolve keyword with promise?

Upvotes: 28

Views: 25402

Answers (2)

kiran gurung
kiran gurung

Reputation: 1

Promise.all() accepts arrays of methods (async) and data can be assign to variables as array

Example:

let [axiosHomeData , axiosUserData] = await Promise.all([axiosHome,  axiosUserData]);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370759

Promise.all accepts a single argument, which is an array of Promises - subsequent arguments are discarded. So, pass in an array instead:

Promise.all([axiosHome,  axiosUserData])
  .then(...

when is that we use resolve keyword with promise?

resolve is not a keyword, it's just the conventional function name used when constructing a Promise:

const prom = new Promise((resolve, reject) => {
  // do some asynchronous stuff
  if (ok) resolve();
  else reject();
});

When a Promise is explicitly constructed like that, call resolve() to resolve the Promise. (of course, one could name the function argument anything, it doesn't have to be called resolve)

Upvotes: 48

Related Questions