-1

I am trying to make a callback function that has an anonymous function nested inside. My code looks something like this:

    function submitGuess(guess) {
        if (guess.length === 5) {
            console.log("The guess was 5 letters");
            const postGuess = async () => {
                console.log("Anon function initiated")
                const res = await fetch(SOME_URL);
            }
        }
    }
    submitGuess(guess)

But the anonymous function never initiates. The second console.log never runs, and I can't figure out why.

4
  • 6
    The posted code defines but never invokes the nested function. Somebody has to say postGuess(). Note that, even after doing that, the value of res, the result of the fetch() will be discarded. Commented May 23, 2022 at 23:48
  • Where and how are you calling submitGuess()? Where and how are you calling postGuess()? Commented May 24, 2022 at 0:44
  • I have just updated the code. The submitGuess(guess) is called in the js file, but still, it won't run the anonymous code. I thought that by simply defining a cost to a anonymous function, it would run, but it still doesn't. Commented May 24, 2022 at 11:00
  • No. Defining a function does define it, not call it. Commented May 24, 2022 at 16:49

1 Answer 1

0

NEW ANSWER: THANKS FOR FEEDBACK

// declare the function
async function submitGuess(guess) {
    if (guess.length === 5) {
        console.log("The guess was 5 letters");
        const postGuess = async () => {
            console.log("Anon function initiated")
            const res = await fetch("url");
        };
        await postGuess(); // call the function
    }
}

submitGuess("input");

OLD: Make sure you declare the functions then you call them !!

// declare the function
function submitGuess(guess) {
    if (guess.length === 5) {
        console.log("The guess was 5 letters");
        postGuess(); // call the function
    }
}

// declare the function
const postGuess = async () => {
    console.log("Anon function initiated")
    const res = await fetch(SOME_URL);
}

submitGuess(guess)
Sign up to request clarification or add additional context in comments.

4 Comments

One needs to await the result of the asynchronous postGuess function within submitGuess which makes the latter an async function as well. But actually from the OP's original example code one does not know yet whether or where/when the OP wants to return the awaited res result.
Thank you. But isn't it not possible to declare and run anon function within another function?
@HanusáTjaldrafløtti ... of cause it is. And with your example code you show it's possible to declare/assign it. You just don't invoke/call/execute the postGuess function from within the submitGuess function's body.
I believe I have what you want now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.