【发布时间】:2020-08-14 22:34:54
【问题描述】:
我一直在开发一个调用多个 API 的程序,使结果可以在应用程序上查看,然后我还必须跟踪结果,以便为以后的功能进行比较。到目前为止,我已经尝试了很多方法,效果最好,但我当前的代码遇到的问题是应用程序上只有一个响应可见
class GetRequest extends React.Component {
constructor(props) {
super(props);
this.state = {
gemBTCask: null,
gemBTCbid: null,
gemETHask: null,
gemETHbid: null
};
}
async componentDidMount() {
fetch('https://api.gemini.com/v1/pubticker/ethusd')
.then(response => response.json())
.then(data => this.setState({ gemETHask: data.ask }))
//.then(data => this.setState({ gemETHbid: data.bid }));
fetch('https://api.gemini.com/v1/pubticker/ethusd')
.then(response => response.json())
.then(data => this.setState({ gemETHbid: data.bid }));
fetch('https://api.gemini.com/v1/pubticker/btcusd')
.then(response => response.json())
.then(data => this.setState({ gemBTCask: data.ask }));
fetch('https://api.gemini.com/v1/pubticker/btcusd')
.then(response => response.json())
.then(data => this.setState({ gemBTCask: data.ask }));
}
render() {
const { gemBTCask } = this.state;
const { gemBTCbid } = this.state;
const { gemETCask } = this.state;
const { gemETCbid } = this.state;
return (
<div className="card text-center m-3">
<div className="card-body">
gemini BTC bid: $: {gemBTCbid}
</div>
<div className="card-arm">
gemini BTC ask: $: {gemBTCask}
</div>
<div className="card-ear">
gemini ETC bid: $: {gemETCbid}
</div>
<div className="card-leg">
gemini ETC ask: $: {gemETCask}
</div>
</div>
);
}
}
【问题讨论】:
-
查看
Promise.all...