即使已经解决,承诺也会挂起

我正在使用request-promise模块来检查网站是否与代理工作。 我试图find代理速度足以在5秒内回答。 因此,如果请求在5秒内没有超时,我只添加对象。

对于某些代理,即使承诺解决,节点脚本暂停一段时间。 我找不到拖延的原因。 我看到它打印Done但它挂起。 1分10秒后,脚本退出。 这是挂起由于我的代码,或操作系统问题打开套接字等?

 const rp = require('request-promise'); const testProxies = [ { "ipAddress": "80.241.219.83", "port": 3128, }, { "ipAddress": "45.55.27.246", "port": 80 }, { "ipAddress": "144.217.197.71", "port": 8080, }, { "ipAddress": "104.131.168.255", "port": 80, }, ]; function CheckSites(sitesArray,site) { let ps = []; for (let i = 0; i < sitesArray.length; i++) { let proxy = sitesArray[i]; let ops = { method: 'GET', resolveWithFullResponse: true, proxy: 'http://' + proxy.ipAddress + ':' + proxy.port, uri:site, }; let resp = rp.get(ops); ps.push(resp); } return Promise.all(ps.map(function (p) { p.time = Date.now(); return p .then(function (a) { return {'header':a.headers,'time':Date.now() - p.time}; }) .timeout(5000) .catch(function (e) { return {}; }) })) } CheckSites(testProxies,'https://www.example.com').then(function (object) { console.log('Done!'); console.log(object); }).catch(function (err) { console.log('Exception: ' + err); }); 

为了您的使用情况,我build议您使用Promise.race()它的行为如Promise.all但是只要最快的代理响应,您就会得到callback。

我已经调查了更多的错误,这似乎是一个request模块的问题 ,当你使用超时,他们只是不closures连接,它处于挂断状态