Как сохранить порядок выборки в порядке - PullRequest
0 голосов
/ 15 октября 2018

У меня есть еще два запроса на одну страницу, как их упорядочить по одному?

Например, просто код, ожидающий, что очередь выборки выполняется в порядке нумерации.

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)

1 Ответ

0 голосов
/ 18 октября 2018

Просто верните 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');
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...