Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using async/await? #120

Open
CrimsonVex opened this issue Apr 30, 2020 · 3 comments
Open

Using async/await? #120

CrimsonVex opened this issue Apr 30, 2020 · 3 comments

Comments

@CrimsonVex
Copy link

How can I use this library using async/await syntax?

@CrimsonVex
Copy link
Author

CrimsonVex commented Apr 30, 2020

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();

@chill117
Copy link
Owner

chill117 commented May 19, 2020

Hello, @Sasstraliss
There are a couple problems with the code snippet above:

  • The module name is "proxy-lists" not "ProxyLists" - see the require() 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.

@0-don
Copy link

0-don commented Mar 23, 2021

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants