Struggling with Promises related to triggering a query then acting on the data

Hello! I'm really struggling with Promises... I've got a situation where I want a form to update a database then based on the update it will run some Javascript to decide what the status of an application should be. For some reason I can't make it happen. I'll make a change to the database, hit Run and it will show a prior result, waiting for me to hit Run again to get the most recent data. getReviewStatusData.trigger is a Retool database SELECT query. I've tried it with a .next after instead of onSuccess and get the same result where getReviewStatus will run successfully (0.004s) and getReviewStatusData will run successfully (0.516s) afterwards.

Any brilliant ideas? Thanks in advance.

function determineReviewStatus(decisions) {
  let {
    review_text_1_decision, review_text_2_decision, review_video_1_decision,
    review_video_2_decision, review_text_decision, review_video_decision,
    review_final_decision
  } = decisions;

  // Additional checks for text decisions
  if (review_text_1_decision === review_text_2_decision) {
    review_text_decision = review_text_1_decision;
  } else if (review_text_1_decision && review_text_2_decision) {
    review_text_decision = 'Maybe';
  }

  // Additional checks for video decisions
  if (review_video_1_decision === review_video_2_decision) {
    review_video_decision = review_video_1_decision;
  } else if (review_video_1_decision && review_video_2_decision) {
    review_video_decision = 'Maybe';
  }

  // Existing status determination logic
  if (!review_text_1_decision && !review_text_2_decision && !review_video_1_decision &&
      !review_video_2_decision && !review_text_decision && !review_video_decision &&
      !review_final_decision) {
    return 'not_started';
  }

  if ((!review_text_1_decision || !review_text_2_decision) &&
      !review_text_decision && !review_final_decision) {
    return 'text';
  }

  if (review_text_decision === 'Yes' &&
      (!review_video_1_decision || !review_video_2_decision) &&
      !review_video_decision && !review_final_decision) {
    return 'video';
  }

  if (review_text_decision === 'Yes' && review_video_decision === 'Yes') {
    return 'final';
  }

  if (review_text_decision === 'Maybe' || review_video_decision === 'Maybe') {
    return 'wait_list';
  }
  
  // Add a default case if no other conditions are met
  return 'review_in_progress';
}
//getReviewStatusData.reset();

getReviewStatusData.trigger( { onSuccess: () => {
  
//  })
//.then(() => {
// console.log("Trigger resolved, data:", getReviewStatusData.data); // Check if data is as expected

  const decisions = {
    review_text_1_decision: getReviewStatusData.data.review_text_1_decision[0],
    review_text_2_decision: getReviewStatusData.data.review_text_2_decision[0],
    review_video_1_decision: getReviewStatusData.data.review_video_1_decision[0],
    review_video_2_decision: getReviewStatusData.data.review_video_2_decision[0],
    review_text_decision: getReviewStatusData.data.review_text_decision[0],
    review_video_decision: getReviewStatusData.data.review_video_decision[0],
    review_final_decision: getReviewStatusData.data.review_final_decision[0]
  };

  const review_status = determineReviewStatus(decisions);
  console.log(review_status); // Output will be one of the review statuses based on the conditions
  return review_status;
}
})

Hi @rstrauss,

It sounds like you're trying to update a database and then use the updated data to determine the status of an application. However, you're not seeing the updated data immediately after running the query.

The issue here is that you're not properly chaining your Promises. Promises are asynchronous operations, which means that they don't execute immediately. Instead, they execute at some point in the future. When you chain Promises, you're essentially telling JavaScript to wait for the first Promise to resolve before executing the second Promise.

In your case, you're not chaining your Promises correctly. You're trying to access the data from the getReviewStatusData query before the query has actually resolved. This is why you're not seeing the updated data immediately.

To fix this, you need to chain your Promises correctly. Here's an example of how to do this:

getReviewStatusData.trigger()
  .then(() => {
    const decisions = {
      review_text_1_decision: getReviewStatusData.data.review_text_1_decision[0],
      review_text_2_decision: getReviewStatusData.data.review_text_2_decision[0],
      review_video_1_decision: getReviewStatusData.data.review_video_1_decision[0],
      review_video_2_decision: getReviewStatusData.data.review_video_2_decision[0],
      review_text_decision: getReviewStatusData.data.review_text_decision[0],
      review_video_decision: getReviewStatusData.data.review_video_decision[0],
      review_final_decision: getReviewStatusData.data.review_final_decision[0]
    };

    const review_status = determineReviewStatus(decisions);
    console.log(review_status);
  });

In this example, the then() method is chained to the trigger() method. This means that the then() method will only be executed after the trigger() method has resolved. This will ensure that you have access to the updated data from the getReviewStatusData query before you try to determine the status of the application.

I hope this helps!

:grinning:

Patrick

1 Like

Thanks Patrick! I was banging my head against the desk, thought I tried that before but apparently not well enough. That did the trick :slight_smile:

1 Like

Glad to hear!

Happy Retool'ing on this Sunday!

:grinning:

Patrick