uncaught in promise with catch present

I am trying to implement promises with recursion. It seems to be working fine, atleast the output is what i want but there is a error in the console i see Uncaught (in promise) paused

The code starts with a call to apply, below is the relevant part of the code

I need to break out of the promise chain in case a div has a class called paused

  const apply = (id, counter) => {
    loadWorkflow(id)
      .then((res) => setVars(res))
      .then((res) => applyFilters(counter))
      .then(null, function(err) {
        console.log('in here errro handler..');
        console.log(err);
      });
  };

  const applyFilters = (counter = 0) => {
    return new Promise((resolve, reject) => {
      currentCounter = counter;
      if(isPaused()) {
        reject(paused);
      } else {
        let filter = filters[counter];
        if(filter) {
          applyStep(filter).then(promiseTimeout(filter.delay_ms)).then(() => applyFilters(++counter)).then(() => resolve(true))
        } else {
          resolve(true);
        }
      }
    })
  };

  const applyStep = (step) => {
    if(step.step_type == 'filter') {
      return worksheet.applyFilterAsync(step.name, values, 'replace');
    } else {
      return new Promise((resolve, reject) => {
        resolve(true)
      });
    }
  };

  const isPaused = () => {
    return jQuery(snackbarPause).hasClass('paused');
  }

Here is the error screenshot

[![enter image description here][1]][1]

I am not sure what is cauing the uncaught, any help on how to fix this would be really great, Thanks.

#update 1


  const applyFilters = (counter = 0) => {
      currentCounter = counter;
      if(isPaused()) {
        return Promise.reject(paused);
      } else if(isClosed) {
        return Promise.reject(closed);
      } else {
        let filter = filters[counter];
        if(filter) {
          return applyStep(filter).then(() => applyFilters(++counter)).then(null, function(err) {
            //reject(err);
            console.log('rethrowing....');
            throw(err);
          })
        } else {
          return Promise.resolve(true);
        }
      }
  };

  const applyStep = (step) => {
    printInfo(step);
    if(step.step_type == 'filter') {
      return worksheet.applyFilterAsync(step.name, values, 'replace');
    } else if(step.step_type == 'parameter') {
      return workbook.changeParameterValueAsync(`${step.name}`, value);
    } else {
      return new Promise((resolve, reject) => {
        resolve(true)
      });
    }
  };

  const apply = (id, counter) => {
    loadWorkflow(id)
      .then((res) => setVars(res)).catch(function(err) {console.log(err);return Promise.reject(err)})
      .then((res) => applyFilters(counter)).catch(function(err) {console.log(`one is ${err}`); return Promise.reject(err)})
      .then((res) => { console.log(res); return activateSheet(activeSheet) }).catch(function(err) {console.log(`one is ${err}`);return Promise.reject(err)})
      .then((res) => updateUseCaseView(useCaseViewId, 'success')).catch(function(err) {console.log(`one is ${err}`);return Promise.reject(err)})
      .then((res) => showRefreshBtn()).catch(function(err) {console.log(`one is ${err}`);return Promise.reject(err)})
      .then(null, function(err) {
        console.log('in here errro handler..');
        console.log(err);
      });
  };

Update #2

  //TBD: send the values with comma separated from rails.
  const applyFilters = (counter = 0) => {
      currentCounter = counter;
      if(isPaused()) {
        return Promise.reject(paused);
      } else if(isClosed) {
        return Promise.reject(closed);
      } else {
        let filter = filters[counter];
        if(filter) {
          return applyStep(filter).then(null, function(err) {
            console.log('here....');
            throw (err);
          }).then(promiseTimeout(filter.delay_ms)).then(() => applyFilters(++counter)).then(null, function(err) {
            console.log('????');
            throw(err);
          })
        } else {
          console.log('in else here....');
          return Promise.resolve(true);
        }
      }
  };


92 thoughts on “uncaught in promise with catch present”

Leave a Comment