Всем привет.
Есть код:
import https from 'https'
async function runGetRequest () {
return new Promise((resolve, reject) => {
const req = https.get({
hostname: 'jsonplaceholder.typicode.com',
method: 'GET',
path: '/todos/1'
}, (resp) => {
console.log(`statusCode: ${resp.statusCode}`)
const data = []
resp.on('data', (chunk) => {
data.push(chunk)
})
resp.on('end', () => {
resolve(Buffer.concat(data).toString())
})
resp.on('error', (err) => {
reject(err)
})
}).on('error', (err) => {
reject(err)
}).on('timeout', (err) => {
reject(err)
req.abort()
}).on('uncaughtException', (err) => {
reject(err)
req.abort()
})
})
}
async function runRequest () {
return new Promise((resolve, reject) => {
const req = https.request({
hostname: 'jsonplaceholder.typicode.com',
method: 'GET',
path: '/todos/1'
}, (resp) => {
console.log(`statusCode: ${resp.statusCode}`)
const data = []
resp.on('data', (chunk) => {
data.push(chunk)
})
resp.on('end', () => {
resolve(Buffer.concat(data).toString())
})
resp.on('error', (err) => {
reject(err)
})
}).on('error', (err) => {
reject(err)
}).on('timeout', (err) => {
reject(err)
req.abort()
}).on('uncaughtException', (err) => {
reject(err)
req.abort()
})
})
}
console.log(await runGetRequest())
console.log(await runRequest())
Запускаю так:
node --harmony-top-level-await --es-module-specifier-resolution=node --enable-source-maps --inspect r.js
Функция runGetRequest - отрабатывает, а runRequest отваливается с :
Error: socket hang up
at connResetException (node:internal/errors:691:14)
at TLSSocket.socketOnEnd (node:_http_client:471:23)
at TLSSocket.emit (node:events:406:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections
(node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
}
Почему? Где ошибка?
Помогите…
Спасибо.