Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS返回不';不要等待等待_Javascript_Ecmascript 2017 - Fatal编程技术网

Javascript JS返回不';不要等待等待

Javascript JS返回不';不要等待等待,javascript,ecmascript-2017,Javascript,Ecmascript 2017,我正在尝试使用异步等待 我的应用程序是这样启动的 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 jobLi

我正在尝试使用异步等待

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

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))
问题是
作业列表
总是作为空数组返回。 下面是
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
关键字放在它前面

我把
async
关键字放在每个
的回调中

并将
wait
放在所有异步操作之前

每个
的回调中的
console.log
打印填充了值的数组

但是
返回似乎在
每个

即使前面有
wait
关键字

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

async function getJobDetailsHTML (url) {
    // ANOTHER AWAIT HERE
    return await axios.get(url).data
}

我认为
jobsInDom。每个
都是同步函数,所以把wait放在前面不会给你想要的效果。因此,不知何故,您需要在所有作业的处理完成时得到解决的承诺,如下所示:

  // 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);