代码之家  ›  专栏  ›  技术社区  ›  Hayk Safaryan

js return不等待wait[重复]

  •  0
  • Hayk Safaryan  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我正在尝试使用异步等待。

    我的应用程序是这样启动的

    axios.get(`${ROOT_URL}/ccidxann.php`)
      .catch(err => console.error('axios error get', err))
      .then(async res => {
        const html = res.data
        const jobList = await getJobList(html)
        console.log(' after each jobList', jobList)
        // jsonfile.writeFileSync('jobs.json', jobList, { flag: 'a' })
      })
      .catch(err => console.error(err))
    

    问题是 jobList 总是作为空数组返回。 这里是 getJobList 功能

    async function getJobList (html) {
      const jobList = []
    
      const dom = new JSDOM(html)
      const { window } = dom
      const $ = require('jquery')(window)
      const jobsInDom = $('table').first().find('td > a')
    
      // AWAIT HERE
      await jobsInDom.each(async function (index, jobElem) {
        const name = $(jobElem).text()
        const url = $(jobElem).attr('href')
        // Another AWAIT HERE
        const jobDetailsHTML = await getJobDetailsHTML(`${ROOT_URL}/${url}`)
          .catch(err => console.error('getJobDetailsHTML err', err))
        const domDetails = new JSDOM(jobDetailsHTML)
        const { window: windowDetails } = domDetails
        const $details = require('jquery')(windowDetails)
        const jobDetailsHTMLBody = $details('body').prop('outerHTML')
        jobList.push({
          _id: url,
          name,
          detailsHTML: jobDetailsHTMLBody
        })
        console.log('in each jobList', jobList)
      }) //each
      return jobList
    }
    

    如你所见我把 async 前面的关键字,

    我已经把 异步的 回调中的关键字 each

    await 在所有异步操作之前。

    这个 console.log 回拨 每个 打印填充值的数组

    但是看起来 return 每个

    即使有 等待 前面的关键字。

    这里还有 getJobDetailsHTML 以防万一。这是一个简单的函数,似乎工作得很好

    async function getJobDetailsHTML (url) {
        // ANOTHER AWAIT HERE
        return await axios.get(url).data
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   udalmik    6 年前

    我想 jobsInDom.each 是同步功能,所以放在wait before不会给您带来预期的效果。所以在某种程度上,当你完成所有工作的时候,你需要得到解决的承诺,比如:

      // handles one element and returns promise
      const jobHandler = async jobElem => {
        const name = $(jobElem).text()
        const url = $(jobElem).attr('href')
        const jobDetailsHTML = await getJobDetailsHTML(`${ROOT_URL}/${url}`)
          .catch(err => console.error('getJobDetailsHTML err', err))
        const domDetails = new JSDOM(jobDetailsHTML)
        const { window: windowDetails } = domDetails
        const $details = require('jquery')(windowDetails)
        const jobDetailsHTMLBody = $details('body').prop('outerHTML')
        jobList.push({
          _id: url,
          name,
          detailsHTML: jobDetailsHTMLBody
        })
        console.log('in each jobList', jobList)
      }
    
      const promises = [];
    
      // start processing of each element
      jobsInDom.each((index, jobElem) => promises.push(jobHandler(jobElem));
    
      // wait for processing of all job elements
      await Promise.all(promises);