javascript - 被调用函数中的等待方法与直接记录的相同方法之间的返回不同
问题描述
我有遍历数组的 for 循环。在每次迭代期间,我希望循环返回所提供网页的值并将其记录到控制台。
for (let i = 0; i < jsonObjSplit.length; i++) {
console.log(getChangeTicketNum(i));
const chgNum = getChangeTicketNum(i);
console.log(getReleaseObj(i, chgNum));
const release = getReleaseObj(i, chgNum);
let currentURL = await gotoUrl(release);
console.log(currentURL);
}
我调用了一个异步函数gotoUrl(release)
,它传递一个包含 url 的对象。
async function gotoUrl(release) {
try {
return await page.goto(release.url, {waitUntil: 'networkidle0'});
} catch (e) {
console.log(e);
}
}
运行我的代码后,我收到null
了回报。
例子:
null
CHG1234567
{
ticket: 'CHG1234567',
id: '123',
url: 'https://exampleurl.net/1234'
}
但是,当我直接在 for 循环中调用该方法时,我会收到返回的正确 URL 信息。
非空返回:
for (let i = 0; i < jsonObjSplit.length; i++) {
console.log(getChangeTicketNum(i));
const chgNum = getChangeTicketNum(i);
console.log(getReleaseObj(i, chgNum));
const release = getReleaseObj(i, chgNum);
console.log(await page.goto(release.url, {waitUntil: 'networkidle0'}));
}
我是 aync/await 的新手,所以我不确定为什么会这样。
解决方案
我得到了“正确”工作的功能。为此,我调用了完全相同的函数 a 两次。第一次登录null
到控制台。第二次返回url信息。
//For each item in splunk export file...
for (let i = 0; i < jsonObjSplit.length; i++) {
console.log(getChangeTicketNum(i));
const chgNum = getChangeTicketNum(i);
console.log(getReleaseObj(i, chgNum));
const release = getReleaseObj(i, chgNum);
let currentURL = await gotoXlr(release);
console.log(currentURL);
let currentURL2 = await gotoXlr(release);
console.log(currentURL2);
}
输出:
null
<ref *1> HTTPResponse {
_contentPromise: null,
_headers: {
date: 'Fri, 30 Jul 2021 13:49:01 GMT',
'strict-transport-security': 'max-age=31536000; includeSubDomains',
'x-xss-protection': '1; mode=block',
'x-content-type-options': 'nosniff',
'cache-control': 'no-cache',
'content-type': 'text/html;charset=utf-8',
'x-frame-options': 'SAMEORIGIN',
vary: 'Accept-Encoding, User-Agent',
'content-length': '6270'
},
_client: CDPSession {
eventsMap: Map(29) {
'Fetch.requestPaused' => [Array],
'Fetch.authRequired' => [Array],
'Network.requestWillBeSent' => [Array],
'Network.requestServedFromCache' => [Array],
'Network.responseReceived' => [Array],
'Network.loadingFinished' => [Array],
'Network.loadingFailed' => [Array],
'Page.frameAttached' => [Array],
'Page.frameNavigated' => [Array],
'Page.navigatedWithinDocument' => [Array],
'Page.frameDetached' => [Array],
'Page.frameStoppedLoading' => [Array],
'Runtime.executionContextCreated' => [Array],
'Runtime.executionContextDestroyed' => [Array],
'Runtime.executionContextsCleared' => [Array],
'Page.lifecycleEvent' => [Array],
'Target.attachedToTarget' => [Array],
'Target.detachedFromTarget' => [Array],
'Page.domContentEventFired' => [Array],
'Page.loadEventFired' => [Array],
'Runtime.consoleAPICalled' => [Array],
'Runtime.bindingCalled' => [Array],
'Page.javascriptDialogOpening' => [Array],
'Runtime.exceptionThrown' => [Array],
'Inspector.targetCrashed' => [Array],
'Performance.metrics' => [Array],
'Log.entryAdded' => [Array],
'Page.fileChooserOpened' => [Array],
Symbol(CDPSession.Disconnected) => []
},
ETC...
为什么会这样,我不确定。该页面可能不是从较早的登录函数加载的,并且在第二次调用该函数时已经完成。我知道这不是一个正确的答案,但由于异步功能是正确的,我决定澄清一下。
推荐阅读
- java - 为什么 javax.tools.ToolProvider.getSystemTool(...) 用最宽的 Error 类而不是 ServiceConfigurationError 重新抛出?
- c - 使用蓝牙在 arduino 上接收带有字符的 2 字节数字时出现问题
- python - 如何在python中验证用户输入的格式是否正确
- android - 聊天应用程序 - 发件人收到通知
- excel - 当目录中存在同名文件时,“对象_workbook的方法保存失败”
- uml - 非软件上下文的 UML 类图的替代方案
- progressive-web-apps - 如何使用独立浏览器分离 PWA 会话和 cookie?(PWA 作为私人标签)
- java - 在实体类中选择属性的日期格式
- python - 如何在 django 模板标签中设置全局可访问的上下文变量?
- javascript - 以下两种实现有什么区别?