skip to Main Content

I have some logic that, rarely – but on occasion, produces erroneous results, and I’m trying to understand why. I’m thinking that it’s likely the case that it’s a timing/order of operations issue.

The function/logic in question looks like this:

  async getAuths(pId, discipline, date) {
    const response = await Promise.all([
      this.db.Authorizations.getByPatientDiscipline(pId, discipline),
      this.db.Patients.getCurrentCovByPatient(pId, date)
    ]);

    const auths = response[0];
    const coverages = response[1];

    for (let i in auths) {
      const auth = auths[i];
      auth.coverage = false;

      // Load coverage if auth has one
      if (auth.coverageId) {
        const covgRes = await this.db.Patients.getByPatientPayer(auth.coverageId);

        // Apply coverage if found
        if (Array.isArray(covgRes) && covgRes.length > 0) {
          auth.coverage = covgRes[0];
        }
      }

      // Todo: Translate to use Array.filter to simplify code
      // Take out coverages with authorizations
      for (let j = coverages.length - 1; j >= 0; j--) {
        if (
          coverages[j].id === auth.coverageId &&
          moment(auth.start, format).isSameOrBefore(moment(date).format(format), "day") &&
          moment(auth.finish, format).isSameOrAfter(moment(date).format(format), "day")
        ) {
          coverages.splice(j, 1);
          break;
        }
      }
    }

    console.log("coverages - withoutAuths 231: ", coverages);

    return {
      authorizations: auths,
      coveragesWithoutAuths: coverages
    };
  }

You’ll notice that I am logging to the console any results remaining in the "coverages" array on line 231. At that point, because coverages WITH authorizations have already been removed in the block above, this array should only contain results that DO NOT have authorizations. However, on occasion I see an object still sitting in that array on line 231 that I know DOES have a valid authorization. So it seems that the logic taking out those elements in the array that do have authorizations is — on occasion — not working, again, perhaps a timing/order of operations issue, or a situation where one object in an array is being polluted by another object in the array.

I’d appreciate any insight as to what the issue may be here.

3

Answers


  1. When you remove an element from a looped array you should always decrement the loop index since the array has got shorter:

    coverages.splice(j--, 1);
    
    Login or Signup to reply.
  2. My theory is that it has to do with the fact that Javascript passes arrays by reference. Have you tried doing a deep clone on arrays or objects do see if it fixes the issue?

    Login or Signup to reply.
  3. This is because of your logic in nested for-loops. Because you provide j as the index for the splice function, the same index may be applied multiple times (after all, each iteration of the higher loop triggers a new loop with j as indexer).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search