简体   繁体   中英

Execute something (strictly) after a loop of (axios) requests

I make a get request with axios, then the result give me N urls where i need to make N different requests to populate a dictionary. The execution of the N requests doesn't need to be sequential and logging data at each iteration i get what i want, by the way I'm not able to access the dictionary after all requests finished. What's the correct way to achieve that?

async function myAsyncFuncion() {
  let  firsturl = "https://www.firsturl.com/something";
  let myDict = {};
  
  axios.get(firsturl).then( res => {
      const $ = cheerio.load(res.data)
      let el_list = $(".someclass");
     
      for(let i=0; i<el_list.length; i++){
  
        let url_ = `https://www.someurl.com/page/${el_list[i]}`;
        axios.get(url_).then( res => { let $$ = cheerio.load(res.data);                                        
                                       let list_s = $$("li.someclass");
                                        
                                        list_s.each((i,e)=> {
                                                let p = $$(e).attr('data-xxx');
                                                myDict[p] = [];
                                                myDict[p].push($$(e).text());
                                                console.log(myDict[p]); //filled as expected
                                              });
                                      
                                      });
        
      }
  });
  
  return myDict;
  }
  
  myAsyncFuncion().then(res => {console.log(res)});  //dict appears empty here

You're code is not awaiting axios.get(...) to resolve.

It is a bad indication when you have an async function without await in it.

Here is how it could work:

async function myAsyncFuncion() {
    let firsturl = "https://www.firsturl.com/something";
    let myDict = {};
  
    const res = await axios.get(firsturl);
    const $ = cheerio.load(res.data);
    let el_list = $(".someclass");
    for(let i=0; i<el_list.length; i++){
        let url_ = `https://www.someurl.com/page/${el_list[i]}`;
        const res = await axios.get(url_);
        let $$ = cheerio.load(res.data);                                        
        let list_s = $$("li.someclass");
        list_s.each((i,e)=> {
            let p = $$(e).attr('data-xxx');
            myDict[p] ??= [];  // Only when entry doesn't exist yet?
            myDict[p].push($$(e).text());
            console.log(myDict[p]);
        });
    }
    return myDict;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM