多个 API 调用的 Node Js 中的套接字挂断错误
Posted
技术标签:
【中文标题】多个 API 调用的 Node Js 中的套接字挂断错误【英文标题】:Socket Hangup error in Nodejs for multiple API calls 【发布时间】:2021-05-20 09:22:06 【问题描述】:我正在尝试从外部 API 获取股票市场中所有上市公司的列表,在获取列表后,我正在尝试获取有关各个公司的所有详细信息,包括图形数据。一切正常。但是,今天我收到套接字挂断错误。我试过在***中浏览其他帖子。但是,它们都不起作用。
const request = require('request');
const fetchAPI = apiPath =>
return new Promise(function (resolve, reject)
request(apiPath, function (error, response, body)
if (!error && response.statusCode == 200)
resolve(body);
else
reject(error);
);
);
// get list of all companies listed in
const fetchCompanyDetails = () =>
return new Promise(function (resolve, reject)
let details = [];
fetchAPI('https://api//')
.then(res =>
res = JSON.parse(res)
details.push(res);
resolve(details);
)
.catch(err =>
console.log("error at fetchcompany details" + err);
)
);
const getDateAndPriceForGraphData = (graphData) =>
let res = []
graphData.forEach(data =>
let d =
d["x"] = new Date(data.businessDate).getTime() / 1000
d["y"] = data.lastTradedPrice
res.push(d)
)
return res
// get graph data for individual assets
const getGraphDataForAssets = (assetID) =>
return new Promise((resolve, reject) =>
let details = ;
fetchAPI(`https://api/$assetID`)
.then(async (res) =>
res = JSON.parse(res)
let data = await getDateAndPriceForGraphData(res)
details = data
resolve(details);
)
.catch(err =>
console.log("error at getGraphDataForAssets" + err);
)
);
// fetch data about individual assets
const fetchAssetDetailsOfIndividualCompanies = (assetID) =>
return new Promise((resolve, reject) =>
let details = "assetData" : , "graphData": ;
fetchAPI(`https://api/$assetID`)
.then(async (res1) =>
res1 = JSON.parse(res1)
details["assetData"] = res1
// get graph data
var graphData = await getGraphDataForAssets(assetID)
details["graphData"] = graphData
resolve(details);
)
.catch(err =>
console.log("error at fetchAssetDetailsOfIndividualCompanies" + err);
reject(err)
)
);
// returns list of details of all tradeable assets (Active and Suspended but not delisted)
const fetchDetailsForEachCompany = async (companyList) =>
let result = []
await Promise.all(companyList.map(async (company) =>
try
// return data for active and suspended assets
if(company.status != "D")
let companyData = await fetchAssetDetailsOfIndividualCompanies(company.id)
result.push(companyData)
catch (error)
console.log('error at fetchDetailsForEachCompany'+ error);
))
return result
exports.fetchAssetDetails = async () =>
let companyDetails = await fetchCompanyDetails()
let det = await fetchDetailsForEachCompany(companyDetails[0])
return det
【问题讨论】:
如果昨天有效,今天无效,请联系您正在使用的 API 的供应商。 顺便说一句,您不需要将new Promise
与await
/async
和.then
混合使用。 (第一个 fetchAPI
需要它;其他函数不需要。)
好吧,我不能,因为不鼓励使用 API 来降低服务器请求。所以,我想知道我的代码是否有问题。
好吧,您正在同时发出所有对公司列表的请求,并且正在非常努力地敲击服务器。谁知道呢,他们甚至可能屏蔽了你。 p-limit
/p-queue
库可以帮助您减少并发请求。
我觉得也是这个原因。感谢图书馆顺便说一句。我会仔细看看的。我想知道您是否可以澄清您对不需要将新承诺与等待/异步混合而感到难过的地方。
【参考方案1】:
为了扩展我不需要那些 new Promise()
s 的意思,这将是上述代码的惯用 async function
重构。
我删除了getGraphDataForAssets
,因为它最终没有被使用; fetchAssetDetailsOfIndividualCompanies
获取了相同的数据(无论如何都是基于 URL),然后让 getGraphDataForAssets
再次获取它。
const request = require("request");
function fetchAPI(apiPath)
return new Promise(function (resolve, reject)
request(apiPath, function (error, response, body)
if (!error && response.statusCode === 200)
resolve(body);
else
reject(error);
);
);
async function fetchJSON(url)
return JSON.parse(await fetchAPI(url));
async function fetchCompanyDetails()
return [await fetchAPI("https://api//")];
function getDateAndPriceForGraphData(graphData)
return graphData.map((data) => (
x: new Date(data.businessDate).getTime() / 1000,
y: data.lastTradedPrice,
));
// fetch data about individual assets
async function fetchAssetDetailsOfIndividualCompanies(assetID)
const assetData = await fetchJSON(`https://api/$assetID`);
const graphData = getDateAndPriceForGraphData(assetData);
return assetID, assetData, graphData ;
// returns list of details of all tradeable assets (Active and Suspended but not delisted)
async function fetchDetailsForEachCompany(companyList)
const promises = companyList.map(async (company) =>
if (company.status === "D") return null;
return fetchAssetDetailsOfIndividualCompanies(company.id);
);
const results = await Promise.all(promises);
return results.filter(Boolean); // drop nulls
async function fetchAssetDetails()
const companyDetails = await fetchCompanyDetails();
return await fetchDetailsForEachCompany(companyDetails[0]);
exports.fetchAssetDetails = fetchAssetDetails;
【讨论】:
以上是关于多个 API 调用的 Node Js 中的套接字挂断错误的主要内容,如果未能解决你的问题,请参考以下文章
Nodejs,Cloud Firestore上传任务 - 身份验证错误:错误:套接字挂起