Promise me This

I have this scoring routine that first checks for unanswered questions, and renders either the review or the score depending on if there are unanswered questions remaining.

The problem is it is running asynchronously, so the score is being calculated even when there are unanswered questions.

I know there are a bunch of hacky ways to fix this, but it seems like a Promise would be a good solution. I’m having trouble understanding what the promise is, if anything needs to be chainned, what needs to be pre-defined and how the syntax should work:

Original Code

handleScore () {
    //check for unanswered
    this.checkUnanswered()

    //score the test
    console.log ("Scoring your test")
    if (this.state.unansweredQuestions.length===0) {
      let score = this.state.selectedAnswers.reduce((totalScore, answer, i) => {
          console.log("Answer " + i + " = " + answer)
          if (answer === this.state.answerKey[i]) {
            totalScore++
          } else {

          }
          return totalScore
        }, 0
      )


      this.setResults(score)
      console.log("Your score is: ", score)
    }
 
  }

  checkUnanswered () {
    let unansweredQuestions = this.state.selectedAnswers.map((answer, i) => {
      if (answer === 0 ) {
        return i
      } else {
        return null
      }
    }).filter((el) => {
      return el
    })

    if (unansweredQuestions.length>0) {
      this.setState({
        unansweredQuestions: unansweredQuestions,
        reviewMode: true
      })
    }
    // console.log("Unanswered: " + unanswered)
    console.log("Unanswered Questions:" + unansweredQuestions)
    console.log("Length of array: " + unansweredQuestions.length)
  }

Have you checked out the new Promise challenges in the ES6 section? If not, I would start there to get the basics of how Promises work.

No I will check those out. Although the first “suggested” thread below my question is one I posted sveral years ago with a similar issue in fetching data from an API call. it’s a long thread and in the end it looks like I chose a different method. Thanks for the heads up about the challenges.