1 Star 1 Fork 2

elung/Web-Study

forked from Observer/Web-Study 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ping.js 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
Rainy120 提交于 2018-02-02 01:13 . Add files via upload
const { URL } = require('url')
const https = require('https')
const pingCount = 5
const paths = {
npm: `https://registry.npmjs.org/vue/latest`,
yarn: `https://registry.yarnpkg.com/vue/latest`,
tb: `https://registry.npm.taobao.org/vue/latest`
}
const ping = url => {
return new Promise((resolve, reject) => {
const start = Date.now()
const { hostname, pathname } = new URL(url)
const req = https.request({
hostname,
path: pathname
}, () => {
process
.stdout
.write('.')
resolve(Date.now() - start)
})
req.on('error', reject)
req.end()
})
}
const pingX = (registry, times) => {
return ping(paths[registry]).then(latency => {
return times > 1
? pingX(registry, times - 1).then(results => [
latency, ...results
])
: [latency]
})
}
const pings = Object
.keys(paths)
.map(registry => {
return pingX(registry, pingCount).then(results => ( { registry, results } ))
})
Promise
.all(pings)
.then(results => {
console.log()
console.log(results)
})
.catch(err => {
console.log(err)
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/elung/Web-Study.git
git@gitee.com:elung/Web-Study.git
elung
Web-Study
Web-Study
master

搜索帮助