Asyc功能等待停止一切,我缺少什么?

发布于 2025-01-19 06:53:17 字数 1406 浏览 1 评论 0原文

我有一个在每个循环结束时被调用的函数,但我在第二次时遇到了麻烦。

async function getData(auth) {
    let sheet = sheetIDGlobal;
    let selectedRow = sheetsRow;
    let sheets = await google.sheets({ version: 'v4', auth });
    console.log("Waiting for Google Sheets reply .....");
    return new Promise((resolve, reject) => {
        sheets.spreadsheets.values.get({
            spreadsheetId: sheet,
            range: selectedRow,
        }, (err, res) => {
            if (err) {
                reject(err);
                console.log('The API returned an error: ' + err);
            }
            const rows = res.data.values;
            if (rows.length) {
                promises = rows.forEach((row) => {
                   // data here
                    console.log(data);
                })
                return Promise.resolve(promises);
            } else {
                console.log('No data found.');
            }
            console.log(rows, "<----rows");
            resolve(rows);
        })
    })
}

在脚本的后面,我有另一个调用 getData() 的函数,但它后面的代码没有运行。

async function loadAddContentPage() {
            await getData(authObj); // this function runs
           // nothing below this line is run
            const createNewPage = 'url-to-get'
            driver.get(createNewPage);
        }

我知道我的 getData() 函数出了问题,并且我认为这与我返回承诺和使用解析的方式有关。

I have a function that gets called at the end of each loop, but I'm having trouble with the 2nd time around.

async function getData(auth) {
    let sheet = sheetIDGlobal;
    let selectedRow = sheetsRow;
    let sheets = await google.sheets({ version: 'v4', auth });
    console.log("Waiting for Google Sheets reply .....");
    return new Promise((resolve, reject) => {
        sheets.spreadsheets.values.get({
            spreadsheetId: sheet,
            range: selectedRow,
        }, (err, res) => {
            if (err) {
                reject(err);
                console.log('The API returned an error: ' + err);
            }
            const rows = res.data.values;
            if (rows.length) {
                promises = rows.forEach((row) => {
                   // data here
                    console.log(data);
                })
                return Promise.resolve(promises);
            } else {
                console.log('No data found.');
            }
            console.log(rows, "<----rows");
            resolve(rows);
        })
    })
}

Later in the script, I have another function that calls getData(), but the code after it doesn't get run.

async function loadAddContentPage() {
            await getData(authObj); // this function runs
           // nothing below this line is run
            const createNewPage = 'url-to-get'
            driver.get(createNewPage);
        }

I know something is wrong with my getData() function, and I assume it's something to do with how I'm both returning a promise and also using resolve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2025-01-26 06:53:17

只需要删除返回Promise.resolve(Promises);

Just needed to remove the return Promise.resolve(promises);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文