proxy-lists
proxy-lists copied to clipboard
Using async/await?
How can I use this library using async/await syntax?
Might've worked it out:
const ProxyLists = require('proxy-lists');
const getProxyList = options => {
return new Promise((resolve, reject) => {
ProxyLists.getProxies(proxyListOptions).on('data', function(_proxies) {
resolve(_proxies);
}).on('error', function(_error) {
reject(_error);
});
});
};
var proxyList = await getProxyList();
Hello, @Sasstraliss There are a couple problems with the code snippet above:
- The module name is
"proxy-lists"
not"ProxyLists"
- see therequire()
statement. - The
"data"
event will fire many times - not just once
If you really want to use async/await you can buffer the proxies you've received via the "data"
event and then resolve your promise with all the proxies.
This is an example for an async function
const ProxyLists = require('proxy-lists');
const getProxyList = () => {
return new Promise(resolve => {
let proxies = [];
ProxyLists.getProxies({ protocols: ['https'] })
.on('data', p => p.forEach(p => proxies.push(`${p.ipAddress}:${p.port}`)))
.once('end', () => resolve(proxies));
})
};
const run = async () => {
const proxyList = await getProxyList();
console.log(proxyList);
};
run();