0
app.get('/god', (req, res) => {
        let year = 2533;
        let preyearlist = [];
        let preyearsuperlist = [];
        let yearlist = [];
        let nextyear = new Date().getFullYear()+543;
        let channel = [];
        while(year <= nextyear){
            channel = []
            for (let i=0; i < 10; i++) {
                preyearsuperlist = [];
                preyearlist = [];
                let peryear = [];
                let ayear = year+i
                fetch('https://www.myhora.com/%E0%B8%AB%E0%B8%A7%E0%B8%A2/%E0%B8%9B%E0%B8%B5-'+ayear+'.aspx')
                .then(res => res.text())
                .then((body) => {
                    let $ = cheerio.load(body);
                })
            }
            year += 10
        }
        res.send(yearlist)
})

while loop end before get all html code from fetch

i don't know how to wait html code from fetch and load into cheerio

i try with setTimeout but is not working

RBP
  • 27
  • 1
  • 4
  • 1
    Use `await fetch()...` to get your loop to pause and wait for the `fetch()` calls (which also requires you to make the request handler `async`. You also don't show any code that actually does anything to `yearList` so there must be something else missing here too. And, add `try/catch` around the function to catch errors in your `await fetch()`. – jfriend00 May 26 '21 at 05:11
  • @jfriend00 oh thank you. i will try that. – RBP May 26 '21 at 05:44
  • Just move `res.send` inside of the last `then` – pguardiario May 26 '21 at 05:58
  • @pguardiario - That won't work because this is a `while` and `for` loop. They need to call `res.send()` only once after they've accumulated all results. – jfriend00 May 26 '21 at 06:04
  • @jfriend - yes you're right. that's a lot of requests. – pguardiario May 26 '21 at 08:04

0 Answers0