How do I update the values of objects outside of a function?

Viewed 23

I am working on a personal Alexa skill that does a bit a web scraping but I can not figure out why my objects outside of the axios.get(url).then({}) function call do not update. Here is the section of the code in question. When I do a console.log(speakOutput) and a console.log(output[2]) inside the function call, it gives a correct output. However, when I make the same two console.log calls afterward the objects have not been updated. Any ideas? Thank you in advance!

    var speakOutput = "old";
    var output = [];

    axios.get(url).then((response) => {
        if(response.status === 200) {
            const html = response.data;
            const $ = cheerio.load(html);
            $('p').each(function(i, element) {
                output.push($(element).text());
            });
            speakOutput = output[2];
            console.log(speakOutput);
            console.log(output[2]);
        }
    });

    console.log("Afterwards: ");
    console.log(output);
    console.log(speakOutput);

    return handlerInput.responseBuilder
        .speak(speakOutput)
        .reprompt(speakOutput)
        .getResponse();
0 Answers
Related