TypeError:#<Promise>不可迭代

iRo*_*tia 12 javascript ajax promise reactjs es6-promise

我试图使用promise.all进行多个api调用,但由于某种原因,它引发了以下错误

TypeError:#不可迭代

我的Promise很简单(这可能是我第二次使用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)
    })
  }
Run Code Online (Sandbox Code Playgroud)

问题:知道为什么我会出现此错误吗?另外,有人还能解释我们什么时候使用带有promise的resolve关键字吗?

Cer*_*nce 15

Promise.all接受单个参数,该参数是s数组Promise -后续参数将被丢弃。因此,改为传入一个数组:

Promise.all([axiosHome,  axiosUserData])
  .then(...
Run Code Online (Sandbox Code Playgroud)

我们什么时候使用带有promise的resolve关键字?

resolve不是关键字,它只是构造a时使用的常规函数​​名称Promise

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

Promise像这样显式构造a时,调用resolve()来解决Promise。(当然,可以将函数自变量命名为任何东西,而不必调用resolve