![](/img/trans.png)
[英]error when using Promise.all: TypeError: intermediate value is not iterable at
[英]Promise.all Error: Uncaught (in promise) TypeError: #<Promise> is not iterable
因此,我需要点击2个API,并在我分派动作之前等待这两个响应返回。
我正在使用Promise.all
但是Promise.all
以下错误:
index.js:51未捕获(承诺)TypeError:#在Function.all()不可迭代
const fetchPrices = () => Promise.resolve(getPrices());
const fetchSupplies = () => Promise.resolve(getSupply());
const fetchAll = () => Promise.all(fetchPrices(), fetchSupplies()).then((resultsArray) => {
return resultsArray;
});
// GET coins from coinmarketcap Pro API v1.
export const startGetPrices = () => dispatch => fetchAll().then((res) => {
console.log('res', res);
//...
});
Promise.all
接受的阵列 Promises
,不Promises
在参数列表中彼此之后列出。 改成:
const fetchAll = () => Promise.all([
fetchPrices(),
fetchSupplies()
]);
注意
.then((resultsArray) => {
return resultsArray;
});
是多余的; 现有的Promise
解析为一个结果数组,因此在其上调用.then
来将另一个 Promise
到其上,并接受该结果数组并解析为该数组没有任何用处; 您可以完全忽略它。
另外,也不需要使用Promise.resolve
我不知道getPrices
和getSupply
返回什么,但是如果您将非Promises传递给Promise.all
,则不会抛出任何错误,结果数组将仅包含这些值。 (如果返回了Promise.all
,则Promise.all
将在所有此类Promise.all
都解决后解决。)因此,您可以执行以下操作:
const fetchAll = () => Promise.all([
getPrices(),
getSupply()
]);
(当然,如果getPrices
和getSupply
返回非Promises,那么首先就不需要Promise.all
了)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.