Reactjs 做出承诺。所有人都要等到API和x27;在它完成之前就完成了
我有一个做这件事的个人资料Reactjs 做出承诺。所有人都要等到API和x27;在它完成之前就完成了,reactjs,promise,Reactjs,Promise,我有一个做这件事的个人资料 Promise.all([this.fetchInsights(), this.fetchTouchpoints()]) .then(([insights, touchpoints]) => console.log(insights, touchpoints) ) 以下是fetchInsights() 以下是fetchTouchpoints() 当我运行这个程序时,页面会立即显示undefined,undefined。这是因为
Promise.all([this.fetchInsights(), this.fetchTouchpoints()])
.then(([insights, touchpoints]) =>
console.log(insights, touchpoints)
)
以下是fetchInsights()
以下是fetchTouchpoints()
当我运行这个程序时,页面会立即显示undefined,undefined。这是因为洞察和接触点都是未定义的,因为API的调用尚未完成
当我查看Eclipse中的日志(这是一个Springboot应用程序)时,我看到响应既成功又在控制台中打印出响应。。。但该应用程序不会更新
如何使Promise.all在两个API调用完成后打印日志
我尝试将Promise.all
放入componentDidMount
和render()
任何帮助都将不胜感激 你忘了还钱了 顺便说一句,为了代码可读性,请使用 下面是固定的
fetchInsights
:
fetchInsights = () => {
return fetch(`API_URL${this.queryParams.custId}/${this.queryParams.acctNo}`, {
method: 'GET', // or 'PUT'
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(res => res.json())
.catch(error => console.error('Error:', error));
};
您的
fetchX
函数不会返回它们正在创建的承诺,因此Promise.all
什么都不等待。在Promise.all中分离响应的最佳方法是什么?在那([1,1])之前,我如何做第一个响应,然后是第二个响应?如果您依赖第一个响应,您可以在第一个响应的中调用第二个响应。或者编写一个异步函数并使用wait fetch…
fetchTouchpoints = () => {
fetch('API_URL' + this.queryParams.custId + '/' + this.queryParams.acctNo, {
method: 'GET', // or 'PUT'
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(res2 => res2.json())
.catch(error => console.error('Error:', error));
};
fetchInsights = () => {
return fetch(`API_URL${this.queryParams.custId}/${this.queryParams.acctNo}`, {
method: 'GET', // or 'PUT'
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(res => res.json())
.catch(error => console.error('Error:', error));
};