Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

api.get(...).then(...).catch(...).finally is not a function

I have a React Native API call I am making.

Theoretically it should work -

    import API from "../../utils/API";

  componentDidMount() {
    let merchantId = this.props.merchant.id;
    let api = new API(this.props.gatheredTokens);
    let self = this;
    api.setRetry(10);
    api
      .get("merchantMessages", { repl_str: merchantId })
      .then(response => this.merchantMessageConfiguration(response.data))
      .catch(function (error) {
        console.log(error);
      })
      .finally(function () {
        self.state.list.push(
          <Card
            merchant={self.props.merchant}
            key={self.props.merchant.id}
            bubblemsg={self.state.bubblemsg}
          />
        );
      })
      .finally(function () {
        self.merchantNoticeLoading(self);
      });
  }

However I am getting the following error:

TypeError

What is causing this error? The code looks valid.

Here is what get is:

 get(API, params = this.defaultParams) {
    this.call = "GET";
    let constructedURL = this.constructURL(API, params);
    axiosRetry(axios, { retries: this.retry });
    return axios.get(constructedURL, this.config);
  }
like image 815
Steven Matthews Avatar asked Sep 11 '19 06:09

Steven Matthews


People also ask

Why is .then not a function?

The "then is not a function" error occurs when the then() method is called on a value that is not a promise. To solve the error, convert the value to a promise before calling the method or make sure to only call the then() method on valid promises.

What is .then and .catch in JavaScript?

In summary: then : when a promise is successful, you can then use the resolved data. catch : when a promise fails, you catch the error, and do something with the error information. finally : when a promise settles (fails or passes), you can finally do something.

What is .finally in JavaScript?

catch... finally statements combo handles errors without stopping JavaScript. The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result.

What does .catch do in JS?

A catch -block contains statements that specify what to do if an exception is thrown in the try -block. If any statement within the try -block (or in a function called from within the try -block) throws an exception, control is immediately shifted to the catch -block.


1 Answers

I suggest to use another then instead of using finally. then after catch is works like a finally. don't forget to use at least one catch in your promise chain, in order to handle your instructions failure.

So this two line of code is the same:

api.get(…).then(…).catch(…).then(...)

and

api.get(…).then(…).catch(…).finally(...)
like image 114
Sadegh Teimori Avatar answered Sep 20 '22 12:09

Sadegh Teimori