等待每个元素

wait for each element

我需要等待每个项目,而不是调用另一个函数。

await readCSV(filename).then(fileArray => {
        for (let element of fileArray) { 
            console.log(element)
            searchItem(element) //<--- Wait each element will finish this function()
        }
    })

searchItem()的函数是:

async function searchItem(item) {
    console.log('2) Avvio Browser for ' + item.codice_kromeda)
    await page.goto('url_part1' + item.codice_kromeda + 'url_part2');

    await page.waitForSelector('.si-search-result')
    let list = await page.$$('.si-search-result');

    console.log(list)
    return true
}

由于 searchItem() 已经 returns 一个在搜索完成时解析的承诺,您可以简单地重组以正确使用 await:

const fileArray = await readCSV(filename);
for (let element of fileArray) { 
    console.log(element);
    await searchItem(element);
}