use promise.all to handle multiple fetch instances

This commit is contained in:
c9s 2022-05-18 02:04:39 +08:00
parent 76949ed4f2
commit e196d7dfb1
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 18 additions and 18 deletions

View File

@ -30,7 +30,7 @@ const ReportDetails = (props: ReportDetailsProps) => {
}, [props.runID])
return <Container>
<h2>Back-test Run ${props.runID}</h2>
<h2>Back-test Run {props.runID}</h2>
<div>
<TradingViewChart basePath={props.basePath} runID={props.runID} intervals={["1m", "5m", "1h"]}/>
</div>

View File

@ -84,10 +84,6 @@ const fetchPositionHistory = (basePath, runID, setter) => {
)
.then((response) => response.text())
.then((data) => tsvParse(data, parsePosition()))
// .then((data) => tsvParse(data))
.then((data) => {
setter(data);
})
.catch((e) => {
console.error("failed to fetch orders", e)
});
@ -216,9 +212,6 @@ function fetchKLines(basePath, runID, symbol, interval, setter) {
.then((response) => response.text())
.then((data) => tsvParse(data, parseKline()))
// .then((data) => tsvParse(data))
.then((data) => {
setter(removeDuplicatedKLines(data));
})
.catch((e) => {
console.error("failed to fetch klines", e)
});
@ -306,16 +299,23 @@ const TradingViewChart = (props) => {
return;
}
if (!data || !orders || !markers || !positionHistory) {
fetchKLines(props.basePath, props.runID, 'ETHUSDT', currentInterval, setData).then(() => {
fetchOrders(props.basePath, props.runID, (orders) => {
setOrders(orders);
if (!data) {
const ordersFetcher = fetchOrders(props.basePath, props.runID, (orders) => {
const markers = ordersToMarkets(currentInterval, orders);
setOrders(orders);
setMarkers(markers);
});
const positionHistoryFetcher = fetchPositionHistory(props.basePath, props.runID).then((data) => {
setPositionHistory(data);
});
Promise.all([ordersFetcher, positionHistoryFetcher]).then(() => {
fetchKLines(props.basePath, props.runID, 'ETHUSDT', currentInterval).then((data) => {
setData(removeDuplicatedKLines(data));
})
});
const markers = ordersToMarkets(currentInterval, orders);
setMarkers(markers);
});
fetchPositionHistory(props.basePath, props.runID, setPositionHistory)
})
return;
}
@ -393,7 +393,7 @@ const TradingViewChart = (props) => {
return () => {
chart.current.remove();
};
}, [chart.current, props.runID, data, currentInterval])
}, [props.runID, currentInterval, data])
// see:
// https://codesandbox.io/s/9inkb?file=/src/styles.css