封装async函数中await page.goto返回值与直接调用该方法返回值不一致问题
Hey there! Let's break down why you're seeing null when using your gotoUrl wrapper, but getting the right result when calling page.goto directly.
First, let's diagnose the core issues
Your problem likely stems from one of these two scenarios:
Missing return value in the
catchblock
Whenpage.gotothrows an error (like a failed navigation), yourgotoUrlfunction'scatchblock only logs the error but doesn't return anything. This means the function will implicitly returnundefined, which you might be misreading asnullin the console. When you callpage.gotodirectly, any error would throw immediately (instead of being swallowed), so you either don't hit the error case or notice it right away.pagevariable is out of scope ingotoUrl
IfgotoUrlis defined in a different scope than where yourpageobject was created (e.g.,pageis a local variable in another async function, butgotoUrlis global), thenpagewill beundefinedinsidegotoUrl. Callingpage.gotowould throw an error, trigger thecatchblock, and leave you with anundefinedreturn value (again, appearing asnull).
Another small thing to check: you might be confusing the HTTPResponse object returned by page.goto with the actual page URL. The response object contains the URL (via response.url()), but it's not the URL string itself.
Fixes to try
Let's address these issues step by step:
1. Fix the error handling in gotoUrl
Add a clear return value in the catch block so you know when navigation fails, and optionally return the URL directly if that's what you need:
// Option 1: Return the full response object, with explicit null on failure async function gotoUrl(page, release) { try { const response = await page.goto(release.url, {waitUntil: 'networkidle0'}); return response; } catch (e) { console.error('Navigation failed:', e); return null; // Explicitly return null for failed navigations } } // Option 2: Return just the URL string (if that's what you actually need) async function gotoUrl(page, release) { try { const response = await page.goto(release.url, {waitUntil: 'networkidle0'}); return response ? response.url() : null; } catch (e) { console.error('Navigation failed:', e); return null; } }
2. Ensure page is accessible to gotoUrl
Pass page as a parameter to gotoUrl to avoid scope issues. This makes your function more portable and avoids relying on global variables:
// When calling the function, pass the page object for (let i = 0; i < jsonObjSplit.length; i++) { const chgNum = getChangeTicketNum(i); console.log(chgNum); const release = getReleaseObj(i, chgNum); console.log(release); // Pass page to gotoUrl here const result = await gotoUrl(page, release); console.log(result); }
3. Optimize your loop code
You're calling getChangeTicketNum and getReleaseObj twice per iteration—let's clean that up to avoid redundant work:
for (let i = 0; i < jsonObjSplit.length; i++) { const chgNum = getChangeTicketNum(i); console.log(chgNum); const release = getReleaseObj(i, chgNum); console.log(release); const response = await gotoUrl(page, release); // Log the URL directly if that's what you want console.log(response ? response.url() : 'Navigation failed'); }
That should resolve the null issue and make your code more robust.
内容的提问来源于stack exchange,提问作者JustReflektor

