【问题标题】:Is it okay to call another endpoint inside the then() function? ReactJS Axios可以在 then() 函数中调用另一个端点吗? ReactJS Axios
【发布时间】:2017-05-29 14:38:08
【问题描述】:

我是 React js 的新手,我对我的端点有疑问。 可以在 .then() 函数中调用另一个端点吗? 这是我的代码:

this.props.getReviwerSurvey(surveyId, authToken.id)
  .then(
    () => {
      const {
        reviewerSurvey } = this.state;
      this.props.getAccount(reviewerSurvey && reviewerSurvey.relationships ?
      reviewerSurvey.relationships.participant.id : null);

    },
  );

this.props.getReviewerSurvey 是我的第一个端点,我的第二个端点 this.props.getAccount() 好像不是标准的。如何在不调用回调中的 this.props.getAccount 的情况下获取 reviewerSurvey.relationships.participant.id 的值?如果有其他方法。想法受到高度赞赏。

【问题讨论】:

    标签: reactjs ecmascript-6 react-redux axios


    【解决方案1】:

    您可以从第一个请求返回已解决的承诺并将其传递给下一个then 链,这样您就可以避免回调地狱。

    一些一般的例子:

    this.props.getReviwerSurvey(surveyId, authToken.id)
      .then(() => {
          const { reviewerSurvey } = this.state;
          return reviewerSurvey;
        },
      ).then((reviewerSurvey) => {
         return this.props.getAccount(reviewerSurvey && reviewerSurvey.relationships ?
          reviewerSurvey.relationships.participant.id : null);
      }).then((participantId) => {
          // do something with participantId or other data related to the account
      });
    

    【讨论】:

    • 好的,我会试试这个想法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2015-10-21
    • 2021-02-10
    • 2021-07-14
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多