Ekzo
Ekzo

Reputation: 133

How to terminate function execution on catch?

How to terminate function execution on catch? I assume that return should be put where I put it (.catch( error => {console.log(error);} return)), but that doesn't work. Tell me how to do it right?

    getCustomers: function () {
            let url = '/crm/customer/';

            axios.get(url).then((response) => {
                this.customers = response.data.results;
                if (this.customers.length === 100) {
                    for (let i = 2; i < 100; i++) {
                        axios.get('/crm/customer/?page=' + i).then((response) => {
                            this.c = response.data.results;
                            for (let item of this.c.values()) {
                                this.customers.push(item);
                            }
                        }).catch( global_waiting_stop());

                    }
                }
            }).catch( error => { console.log(error); })
            .finally(() => (global_waiting_stop()));
        },

Upvotes: 2

Views: 826

Answers (2)

Jinal Somaiya
Jinal Somaiya

Reputation: 1971

try below code:

getCustomers: function() {
    let url = '/crm/customer/';
    return axios.get(url).then((response) => {
       this.customers = response.data.results;
       if (this.customers.length === 100) {
          for (let i = 2; i < 100; i++) {
            return axios.get('/crm/customer/?page=' + i).then((response) => {
              this.c = response.data.results;
                for (let item of this.c.values()) {
                  this.customers.push(item);
                }
              }).catch(error => {
                  console.log(error);
                  return false;
              })
          }
        }
     }).catch(error => {
     console.log(error);
     return false;
   })
},

Upvotes: 0

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23389

finally() executes after both the then and the catch functions. If you don't want it to run then move your global_waiting_stop to the bottom of the then block instead and get rid of finally.

getCustomers: function() {
  let url = '/crm/customer/';
  axios.get(url).then((response) => {
    this.customers = response.data.results;
    if (this.customers.length === 100) {
      for (let i = 2; i < 100; i++) {
        axios.get('/crm/customer/?page=' + i).then((response) => {
            this.c = response.data.results;
            for (let item of this.c.values()) {
              this.customers.push(item);
            }
          }).catch(error => {
              console.log(error);
            }
            return)
          .finally(() => (global_waiting_stop()));
      }
    }
    global_waiting_stop();
  }).catch(error => {
    console.log(error);
  })
},

Upvotes: 2

Related Questions