![](/img/trans.png)
[英]Promise.all Error: Uncaught (in promise) TypeError: #<Promise> is not iterable
[英]TypeError: #<Promise> is not iterable
我試圖使用 promise.all 進行多個 api 調用,但由於某種原因它拋出以下錯誤
類型錯誤:# 不可迭代
我的 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)
})
}
問題:知道為什么我會收到這個錯誤嗎? 另外,有人可以解釋一下我們什么時候在promise中使用resolve關鍵字嗎?
Promise.all
接受單個參數,它是一個Promise
數組- 后續參數將被丟棄。 因此,改為傳入一個數組:
Promise.all([axiosHome, axiosUserData])
.then(...
我們什么時候在 promise 中使用 resolve 關鍵字?
resolve
不是關鍵字,它只是構造Promise
時使用的常規函數名稱:
const prom = new Promise((resolve, reject) => {
// do some asynchronous stuff
if (ok) resolve();
else reject();
});
當Promise
像這樣顯式構造時,調用resolve()
來解析 Promise。 (當然,您可以將函數參數命名為任何名稱,不必將其稱為resolve
)
Promise.all() 接受方法數組(異步)並且數據可以作為數組分配給變量
例子:
let [axiosHomeData , axiosUserData] = await Promise.all([axiosHome, axiosUserData]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.