Skip to content

Instantly share code, notes, and snippets.

@IPRIT
Last active August 3, 2019 20:32
Show Gist options
  • Select an option

  • Save IPRIT/bed206abdd8a200d84fec8df763430e8 to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/bed206abdd8a200d84fec8df763430e8 to your computer and use it in GitHub Desktop.
const urls = [
'https://ya.ru',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'https://vk.com',
'http://twosphere.ru',
'https://vc.ru',
'https://javascript-future.io',
'https://google.com',
'https://yandex.ru',
'https://ya.ru',
'https://ya.ru',
'https://ya.ru',
'https://ya.ru',
];
function loadAll (urls, limit, callback) {
let finished = 0;
let processing = 0;
const processed = Object.create( null );
const processingOps = Object.create( null );
const queue = [];
function load (url) {
if (processing >= limit) {
return void queue.push( url );
}
processing++;
const onResultBound = onResult.bind( null, url );
if (url in processed) {
onResultBound( processed[ url ] );
} else if (url in processingOps) {
const promise = processingOps[ url ];
promise.then( onResultBound );
} else {
processingOps[ url ] = fetch(url).then( onResultBound ).catch( onResultBound );
}
}
function onResult (url, result) {
processed[ url ] = result;
processingOps[ url ] = null;
delete processingOps[ url ];
processing--;
finished++;
if (finished >= urls.length) {
callback( getProcessed() );
} else {
next();
}
return result;
}
function next () {
if (!queue.length) {
return;
}
load( queue.shift() );
}
function getProcessed () {
return urls.map(url => {
return processed[ url ];
});
}
for (let i = 0; i < urls.length; ++i) {
load( urls[i] );
}
}
loadAll(urls, 3, function (results) {
console.log( results );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment