可选链接 ( obj?.param1?.param2
) 似乎是一个很棒的功能,我真的很想看到它的实现,并最终摆脱嵌套的 ifs、任意函数以及如此简单的操作所不具备的功能。
但是有一个问题,它不起作用。我更新到 Node 12,但仍然出现错误:
var dude = res?.param?.params[0]
SyntaxError: Unexpected token '.'
Run Code Online (Sandbox Code Playgroud)
或者
var dude = res.param?.params[0]
SyntaxError: Unexpected token '.'
Run Code Online (Sandbox Code Playgroud)
问题是什么?
我是否需要更改某些语言配置或下载库以启用此功能?或者它只是还没有出来?
使用 puppeteer,当我输入一个值时,我会打开一个页面 - 它输出结果。
await page.click('button[class="button form-button rs-gcbalance-btn"]')
await page.waitForSelector('div[class="small-4 large-4 rs-gcbalance-result-num-col').catch(err => console.log(err))
await page.evaluate((card) => {
console.log(card + " - " + document.querySelectorAll('div[class="small-4 large-4 rs-gcbalance-result-num-col"]')[1].querySelector('span').innerHTML)
}, card)
Run Code Online (Sandbox Code Playgroud)
但是只有当输入的值有效时才能正常工作。如果不是,它会抛出一个错误,但没有任何网络活动或加载事件。
这意味着,如果值不正确,我正在等待的元素将不会出现并抛出错误,关闭程序。
Navigation Timeout Exceeded: 30000ms exceeded
Run Code Online (Sandbox Code Playgroud)
问题是:如何处理错误,所以如果它抛出超时错误,我可以捕获它并调用另一个函数?
我有一个旧的 AWS Lambda 函数,它被声明为同步(使用 Promises),声明如下所示:
exports.getSong = (event, context, callback) => { }
Run Code Online (Sandbox Code Playgroud)
它按预期工作。最近,我决定使用 async/await 重写它,因此尝试getSong
异步声明该函数,如下所示:
exports.getSong = async (event, context) => { }
Run Code Online (Sandbox Code Playgroud)
在尝试执行时,我收到以下完整错误:
{
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Unexpected token '.'",
"trace": [
"Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'",
" at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1133:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)",
" at Module.load (internal/modules/cjs/loader.js:977:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:877:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
" at internal/main/run_main_module.js:18:47"
] …
Run Code Online (Sandbox Code Playgroud) javascript amazon-web-services node.js async-await aws-lambda
我有一个数据流,我需要将其写入任何地方,但又不能立即停止它。下面的代码写入一个文件,因此只要流正在运行,它就会保持连接处于活动状态。
request
.get(href)
.on('response', function(response) {
console.log(response.statusCode)
console.log(response.headers['content-type'])
})
.pipe(fs.createWriteStream("name.mp3"))
Run Code Online (Sandbox Code Playgroud)
是否可以将该流传输到任何地方,同时仍然保持连接fs.createWritableStream
?
我尝试使用 dev-null (npm 包),但它似乎会立即终止连接。