1

I have a function that check if the id is <=0 and rejects the promise if it is, otherwise it find an assessment and resolves the promise with an assessment. My problem is that right now when I pass -1 to the function it goes into the .then anyway, even though I expect it not to, which causes problems. I don't quite understand why is this happening.

    AssessmentsService.getClientAssessment($scope.graphAssessment.id)
      .then((assessment) => {
        if ($scope.clientAssessments) {
          $scope.clientAssessments.title.text = 'All Time ' + assessment.shortName;
        }
        if ($scope.thirtyDayAssessments) {
          $scope.thirtyDayAssessments.title.text = '30 Day ' + assessment.shortName;
        }
      })
      .catch((error) => {
        console.log(error);
      });

This is the getClientAssessment function:

  getClientAssessment(id, forceRefresh) {
    if (id <= 0) {
      return this.$q.reject();
    }

    const assessmentId = Number(id);
    const assessment = _.find(this.assessments, (a) => a.id === assessmentId);

    if (!assessment || forceRefresh) {
      return this.authHttp
        .get(`${this.Environment.serverURL}/assessments/${id}`)
        .then(({ data }) => {
          this.assessments.push(data);
          return data;
        })
        .catch((error) => {
          console.error(error);
        });
    }

    return this.$q.resolve(assessment);
  }
Onyx
  • 5,186
  • 8
  • 39
  • 86
  • Your `getClientAssessment` never returns a rejected promise. It already did handle the error by logging it (and returning `undefined` instead). Don't do that. – Bergi Mar 16 '22 at 18:57

0 Answers0