javascript - 如何保持获取队列有序
问题描述
我一页还有两个抓取请求,如何一一排列?
例如,只是一个代码,期望获取队列以 num 顺序执行。
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');
});
推荐阅读
- google-cloud-storage - 谷歌云存储 - 无法覆盖文件
- sql - 在列名称不唯一的位置进行数据透视
- c++ - 多态 r 值引用?
- entity-framework - EF核心左连接从孩子到父母
- amazon-web-services - 加载 AWS Glue S3 源数据
- reactjs - React - 将按钮值传递给(mui)文本字段
- html - 如何显示带有汉堡图标的菜单列表?
- tensorflow - tensorflow 自定义循环不会在第一个 epoch 结束并且进度条运行到无限
- javascript - 在 Google 云端硬盘中创建副本后如何自动打开 Google 表格
- java - 有没有办法将自定义界面集成到 IntelliJ 中?