【问题标题】:How to terminate function execution on catch?如何在捕获时终止函数执行?
【发布时间】:2020-08-28 13:02:48
【问题描述】:

如何在 catch 时终止函数执行?我认为 return 应该放在我放的地方(.catch( error => {console.log(error);} return)),但这不起作用。告诉我怎么做才对?

    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()));
        },

【问题讨论】:

  • 如果我正确地回答了你的问题,如果第二个 axios 失败,你想要退出吗?然后将它们包装在 Promise.all 中并链接它
  • @AniketJha 执行我,但是怎么做呢?我是初学者
  • @AniketJha - Promise.all 仍将同时运行它们。他需要将其转换为异步函数并等待每个调用......但是连续 97 个 ajax 调用......这将需要很长时间。当然,API 有某种分页机制,他们并没有完全像这样猜测。
  • @Iwrestledabearonce。是否有可能提前知道可以获取多少页?我做这一切只是为了从所有页面获取数据,我把 100 留有边距,因为会添加新的
  • @Iwrestledabearonce。首先,您的用户名很棒。是的,Promise.all 会同时进行许多网络调用,是的,在 API 级别应该有一些分页机制来限制调用。我会触发页面 api 调用等待数据然后发出新调用。 Ekzo 是否真的需要在这一点上拥有页面数据?

标签: javascript vue.js axios


【解决方案1】:

finally()thencatch 函数之后执行。如果您不希望它运行,请将您的 global_waiting_stop 移动到 then 块的底部,然后删除 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);
  })
},

【讨论】:

  • 我希望整个函数在 get (second) 崩溃时终止。我需要这样做吗?我更新了问题
  • 问题在于,你有一个循环同时触发多个异步任务。如果其中一个崩溃,其他的仍然会完成。如果您希望能够取消它们,您需要一次取消它们并等待响应,然后再发送更多。
【解决方案2】:

试试下面的代码:

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;
   })
},

【讨论】:

  • for 没有第二次运行,即使在第 3 页我有数据 :(
  • IDE 告诉我“'for' 语句不会循环”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多