我正在尝试使用 async / await 简化代码
但是在https.get
使用 async / await 结构时有问题。
我知道如何使用第三方模块执行此操作,但更喜欢本机 node.jshttps
模块。
下面的代码对我不起作用:
async function get_page() {
const https = require('https')
const url = 'https://example.com'
const util = require('util')
const https_get = util.promisify(https.get)
const data = await https_get(url)
do_awesome_things_with_data(data)
}
Run Code Online (Sandbox Code Playgroud)
此代码工作正常:
function get_page() {
const https = require('https')
const url = 'https://example.com'
let data = ''
https.get(url, res => {
res.on('data', chunk => { data += chunk })
res.on('end', () => {
do_awesome_things_with_data(data)
})
})
}
Run Code Online (Sandbox Code Playgroud) 如何替换N
以下字符串中第一次出现的多个空格和制表符:
07/12/2017 11:01 AM 21523 filename with s p a c e s.js
Run Code Online (Sandbox Code Playgroud)
我期待以下结果:
07/12/2017|11:01|AM|21523|filename with s p a c e s.js
Run Code Online (Sandbox Code Playgroud)
我只通过N
在同一个字符串上调用替换时间来知道不是很优雅的选项
.replace(/\s+/, "|").replace(/\s+/, "|").replace(/\s+/, "|");
Run Code Online (Sandbox Code Playgroud)
值得一提的是,我将在近 1,000,000 行上运行它,因此性能很重要。
我试图找出最快的方法而不影响性能,同时使用Node.js同步或异步移动100.000+文件
我有3个不同的循环由同步和异步测试forEach
async.each
和for
.循环迭代不会对时序结果产生太大影响.影响性能和速度的最重要的逻辑是同步或异步移动文件.
测试用例显示,同步renameSync()
速度较慢,为20%,然后是异步rename()
.同时异步rename()
使用3倍以上的CPU功率,然后同步renameSync()
.
我是否正确地认为使用同步更明智,renameSync()
因为异步中的速度提升rename()
不是那么重要且等于20%.虽然异步版本的CPU利用率开销似乎很大 - 300%?
或者我错过了什么?也许在速度/性能方面有更好的解决方法.
时间安排:
eachAsyncRenameAsync()
node rename.js 1.60s user 11.20s system 280% cpu 4.559 total
node rename.js 1.65s user 11.82s system 284% cpu 4.732 total
node rename.js 1.64s user 11.84s system 292% cpu 4.606 total
eachSyncRenameSync()
node rename.js 0.69s user 5.01s system 97% cpu 5.851 total
node rename.js 0.67s user 4.88s system 97% cpu 5.687 total
node rename.js 0.68s …
Run Code Online (Sandbox Code Playgroud) 我有如下 html 元素,其层次结构未知:
<div class="parent" name="parent">
<div>
<div>
<div>
<div onclick="get_parent(this)">
Nested Child 1
</div>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class="parent" name="parent">
<div>
<div onclick="get_parent(this)">
Nested Child 2
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以按类或名称获取嵌套子项的父项?
也许有一些替代方案,child.parentElement
或者child.parentNode
我不知道哪个可以帮助我?
或者循环遍历所有父母直到我被需要class
或者name
是唯一可能的选择?
我正在尝试8.8.8.8
在Node.js解析查询中设置Google DNS服务器。
正确的方法是什么?在命令行中,通常我们可以执行以下操作:
$ nslookup stackoverflow.com 8.8.8.8
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: stackoverflow.com
Address: 151.101.1.69
Address: 151.101.65.69
Address: 151.101.129.69
Address: 151.101.193.69
Run Code Online (Sandbox Code Playgroud)
但尚不清楚如何在Node.js中采用相同的方法
require('dns').resolve('stackoverflow.com', function (err, addresses) {
console.log(err, addresses);
});
// => null [ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
Run Code Online (Sandbox Code Playgroud)
仅当目标文件不存在时,我才尝试在 Node.js 中异步重命名文件。
我做了一个快速测试,如下所示:
const fs = require('fs')
const files = [ 'file1', 'file2', 'file3' ]
const new_name = 'new-name' // same destination name for all
Run Code Online (Sandbox Code Playgroud)
fs.exists() - 已弃用
for (let file of files)
fs.exists(new_name, (exists) => {
if (!exists) fs.rename(file, new_name, (err) => {})
})
Run Code Online (Sandbox Code Playgroud)
fs.access() - 推荐
for (let file of files)
fs.access(new_name, fs.constants.F_OK, (err) => {
if (err) fs.rename(file, new_name, (err) => {})
})
Run Code Online (Sandbox Code Playgroud)
const fs_extra = require('fs-extra')
for (let file of …
Run Code Online (Sandbox Code Playgroud) 这似乎是显而易见的,但我无法弄清楚如何缩短代码中多个位置的深度嵌套对象值的更改,而无需每次重新键入该值的键路径:
> obj = { 'key1': { 'key2': { 'key3': { 'key4': { 'key5': 'value' }}}}}
> obj.key1.key2.key3.key4.key5
'value'
Run Code Online (Sandbox Code Playgroud)
> obj.key1.key2.key3.key4.key5 = 'changeme'
> obj.key1.key2.key3.key4.key5
'changeme'
> obj.key1.key2.key3.key4.key5 = 'changeme2'
> obj.key1.key2.key3.key4.key5
'changeme2'
Run Code Online (Sandbox Code Playgroud) node.js ×6
javascript ×5
asynchronous ×2
async-await ×1
dns ×1
fs ×1
fs-extra ×1
html ×1
node-async ×1
object ×1
performance ×1
regex ×1
replace ×1