gpt4 book ai didi

javascript - 如何保持抓取队列有序

转载 作者:行者123 更新时间:2023-12-03 00:56:23 25 4
gpt4 key购买 nike

我在一个页面中多了两个抓取请求,如何将它们一一排列?

只是一个示例代码,期望获取队列按 num order 执行。

class FetchingQueue extends Component {
...
componentDidUpdate(preProps) {
if ( this.props.prop1 != preProps.props1) {
this.props.fetching1
this.props.fetching2
this.timeoutHanlde = setTimeout(() => {
this.props.fetching3
}, 1000)
}
}
render() {
return (
...
)
}
}
export default connect(
state => ({
prop1: state.reducer.props1
}),
{ fetching1, fetching2, fetching3 }
)(FetchingQueue)

最佳答案

只需从获取函数返回 Promises,然后等待它们:

class FetchingQueue extends Component {
...
async componentDidMount() {
const fetching1Result = await this.props.fetching1();
const fetching2Result = await this.props.fetching2();
const fetching3Result = await this.props.fetching3();
}

render() {
return (
...
)
}
}

export default connect(
state => ({ prop1: state.reducer.props1 }),
{ fetching1, fetching2, fetching3 }
)(FetchingQueue)

获取函数可以如下所示:

const fetching1 = new Promise((resolve, reject) => {
// call resolve when ready
resolve('result');
});

关于javascript - 如何保持抓取队列有序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com