0

I'm working on a node word guessing game and I'm trying to have code that prompts the user for input until they run out of guesses or get the word right. Right now I'm having an issue working with prompt's callback. Because of the nature of callbacks my code runs through all the "prompt" code for each word before finally waiting for user input. I've tried using a forEach and while loop originally. Later tried setting up promises, but I haven't figured anything out.

for(i = 0; i < wordsInPlay.length; i++){
    if(losses < 3){
        let solved = false;
        wordThisRound = new Word(wordsInPlay[i]);
        wordThisRound.setupWord();
        getGuess();
    }
}

Here's the getGuess function implements the prompt nod.js package:

function getGuess() {
wordThisRound.updateWordDisplay();
if (wrongsLeft > 0) {
    new Promise(function(resolve) {
        prompt.get(guessObject, function(err, result) {
            console.log('line 68');
            if(err) throw err;
            let userGuess;
            const letterGuessed = result.guess.toLowerCase();
            if (wordThisRound.wordObject.hasOwnProperty(letterGuessed) && wordThisRound.wordObject[letterGuessed].guessed === false) {
                userGuess = true;
                wordThisRound.wordObject[letterGuessed].guessed = true;
                wordThisRound.wordObject[letterGuessed].changeDisplay();
            }
            else {
                userGuess = false;
                wrongsLeft--;
            }
            resolve({gussedRight: userGuess, letter: letterGuessed});
        });
    }).then(function(promise){
        console.log('Inside .then function.');
        new Promise(function(resolve){
            gameFeedback(promise.gussedRight, promise.letter);
            resolve();
        });
    }).then(function(){
        getGuess();
    });
}
else console.log("You're our of wrong guesses...You lost this round.");
}
  • 1
    since Promises are by nature asynchronous, and `getGuess` doesn't return anything (let alone a promise), then of course your loop will simply iterate `wordsInPlay.length` times – Bravo Nov 01 '18 at 05:23
  • You seem to be under a mistaken impression that wrapping something in a promise and then blindly calling `resolve()` does something useful. It does not. Promises don't automatically wait for anything. They resolve when you call `resolve()`. They don't wait for anything else to finish unless your code hooks into the completion of the asynchronous event and only calls resolve when that other code has told you it has completed. – jfriend00 Nov 01 '18 at 05:52
  • You have lots of functions in your code that you don't show what they do. We don't know if any of them are asynchronous and, if so, how you would know when they are done. Wrapping a promise around them like you show won't help you. You have to hook into the completion callback of an asynchronous operation to know when it's done. – jfriend00 Nov 01 '18 at 05:54
  • @Bravo, right, that makes sense. I’ll see how I can alter my code to fix that. Thanks! – Josephine Cortez Ayala Nov 02 '18 at 08:01
  • @jfriend00, so I set up the promises in order to use the .then() method. I can upload the code for the rest of the functions if that’ll help, but other than promises I set up and the prompt.get() that’s it for asynchronous functions. – Josephine Cortez Ayala Nov 02 '18 at 08:07
  • @jfriend00 My prompt function is resolving to an object i need in the gameFeedback() function. gameFeedback() was running before prompt finished. Thats why I used promises. The last promose is just there to prevent getGuess() from running agin before the first one finishes. I know my code is convoluted. Is there a better way to accomplish all that? – Josephine Cortez Ayala Nov 02 '18 at 08:15
  • I tried rewriting your code to do the asynchronous things properly, but it's unclear what you're trying to do. Several questions. 1. Why are you calling `getGuess()` both in a `for` loop and calling it recursively from within `getGuess()`? What is the objective of that? 2. Is `gameFeedback()` asynchronous or anything you need to wait for? – jfriend00 Nov 02 '18 at 16:05
  • Thanks for trying to help @jfriend00. 1) I'm calling it recursively because I ditched the while loop I originally implemented because it wouldn't break when the condition of (wrongsLeft === 0) was met. I need to make sure getGuess continues to run until the user guesses all the correct letters or guesses wrong too many times. 2) gameFeedback() isn't asynchronous but it depends on the asynchronous functions in order to log out the results. – Josephine Cortez Ayala Nov 10 '18 at 02:32

0 Answers0