-1
Hi, I’m developing a search engine for a bot rank on Twitch.tv follows the code below
const cheerio = require('cheerio')
const URL ='https://shadowarena.pearlabyss.com/en-US/Arena?battleType=0&server=sa'
let rankYoda;
async function acesso(){
const response = await request(URL)
const $ = cheerio.load(response)
$('.box_list_area').each((i, e) => {
const name = $(e).find('.thum_name').text()
const rank = $(e).find('.thum_rank').text()
if(name === "YoDaSL"){
rankYoda = rank
}
}
)}
acesso()
console.log(rankYoda)
Whenever I run, it returns me on the console the value undefined
, and I would like that when such name "Yodasl" is found, to stop the loop and associate it to a variable outside the loop, in order to give console.log on it, someone could help me?
Hello! You declared the variable
let rankYoda;
before the asynchronous function. So far so good. Right after the function vc ran the function and made a console.log to see the value of the variable. The value of the variable is Undefined pq the function where you want to assign a value to it is asynchronous, ie it is not at the same time that the JS is executed. Do an eye test: look at your code from top to bottom in a fraction if second. The functionacesso()
has already returned something? Certainly not, because it is asynchronous, that is, does not respect the time, depends on the return.– Sam
I understood, but how could I do it then? I’m very beginner in javascript, I did some research but found nothing...
– Caio Oliveira Felix
Use logic and creativity.
– Sam