异步函数中的 Promise.all 错误:undefined 不是函数 [英] Promise.all error inside async function : undefined is not a function

查看:349
本文介绍了异步函数中的 Promise.all 错误:undefined 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的异步函数中,我使用 Promise.all 但由于某种原因它没有定义或者这里的东西是函数

in my async function i use Promise.all but for some reason its not defined or something here is the function

async function check_available_money() {


    global_browser = await puppeteer.launch({headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox']});
    const page = await global_browser.newPage();
    await page.setViewport({width: 1000, height: 1100});
    var setting = {'username': 'aa', 'password': 'bb'};


    try {
        await page.goto('https://example.com/login', {timeout: 90000})
            .catch(function (error) {
                    throw new Error(' TIMEOUT 1 ');
                }
            );

        await page.$eval('#username', (el, setting) => el.value = setting.username, setting);
        await page.$eval('#password', (el, setting) => el.value = setting.password, setting);


        console.log(tab_id + ' -> SUMITING LOGIN FORM  ');
        await Promise.all(
            page.$eval('form', form => form.submit()),
            page.waitForNavigation()
        )


        console.log(tab_id + ' -> SUMITING LOGIN FORM DONE !  ');



    }
    catch (e) {

        await page.close();
        console.log(e);
    }
}

我从这部分得到错误

await Promise.all(
            page.$eval('form', form => form.submit()),
            page.waitForNavigation()
        )

如果我删除 await Promise.all 并输入

if i remove await Promise.all and just type

            await page.$eval('form', form => form.submit());
            await page.waitForNavigation();

一切正常

这里是错误堆栈

TypeError: undefined is not a function
    at Function.all (<anonymous>)
    at check_available_money (D:\wamp\www\withdraw\robot\server.js:115:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13184) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed.
    at Promise (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\Connection.js:202:56)
    at new Promise (<anonymous>)
    at CDPSession.send (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\Connection.js:201:12)
    at ExecutionContext.evaluateHandle (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\ExecutionContext.js:79:75)
    at ExecutionContext.evaluate (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\ExecutionContext.js:46:31)
    at ElementHandle.$eval (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\ElementHandle.js:293:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:13184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:13184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:13184) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (D:\wamp\www\withdraw\robot\node_modules\puppeteer\lib\NavigatorWatcher.js:73:21)
    at <anonymous>
(node:13184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

推荐答案

Promise.all 接受一个可迭代的参数,而不是多个参数.它试图迭代您的第一个参数,但它没有 [Symbol.iterator] 方法 - undefined is not a function".传递一个数组:

Promise.all takes an iterable, not multiple arguments. It tried to iterate your first argument, but that didn't have a [Symbol.iterator] method - "undefined is not a function". Pass an array:

await Promise.all([
    page.$eval('form', form => form.submit()),
    page.waitForNavigation(),
])

这篇关于异步函数中的 Promise.all 错误:undefined 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆