Node.js
1
Вклад в тег
const promiseRace = promises => (
new Promise((fulfil, reject) => {
promises.forEach((promise, index) => {
promise.then(data => fulfil({ data, index }), reject);
});
})
);
class Fetcher {
constructor(maxConnections = 20) {
this.maxConnections = maxConnections;
}
async request(urls, options = {}) {
const responsePromises = [];
for (const url of urls) {
if (responsePromises.length >= this.maxConnections) {
const { data: response, index } = await promiseRace(responsePromises);
responsePromises.splice(index, 1);
}
responsePromises.push(fetch(url, options));
}
return Promise.all(responsePromises);
}
}
// test
(async () => {
try {
const fetcher = new Fetcher(2); // лимит 2
const urls = [];
for (let i = 0; i < 200; i++) {
urls.push(`/api/user/${i}`);
}
const responses = await fetcher.request(urls);
console.log('end');
} catch (e) {
console.error(e);
}
})();
console.log(str.match(/class=\"page_cover crisp_image _page_cover\" style=\"background-image: url\((https:\/\/.*jpg)/)[1]);