Skip to content

Instantly share code, notes, and snippets.

@nikpros
Last active February 13, 2024 11:53
Show Gist options
  • Save nikpros/0fb75e7bf84e7bd47155659061e1270b to your computer and use it in GitHub Desktop.
Save nikpros/0fb75e7bf84e7bd47155659061e1270b to your computer and use it in GitHub Desktop.
test task by Yandex/VK tech
queueCb(
[
'https://jsonplaceholder.typicode.com/posts/1',
'https://jsonplaceholder.typicode.com/posts/2',
'https://jsonplaceholder.typicode.com/posts/4',
'https://jsonplaceholder.typicode.com/posts/4',
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/posts/3',
'https://jsonplaceholder.typicode.com/posts/1',
],
3,
res => console.log('finish', res)
);
function queueCb(urls, limit, cb) {
urls = Array.from(new Set(urls));
const results = new Array(urls.length).fill(null);
let counter = 0;
new Array(limit).fill(null).forEach(() => requestWrapper(urls.shift()));
function requestWrapper(url) {
const index = counter;
counter++;
fetch(url)
.then(response => response.json())
.then(result => {
results[index] = result;
if (results.every(Boolean)) {
cb(results);
} else {
requestWrapper(urls.shift());
}
});
}
}
@nikpros
Copy link
Author

nikpros commented Jan 18, 2024

есть подозрение, что 4 требование не выполняется, боюсь, что 23-24 строчки не особо этому способствуют :)

P.S. может стоит попробовать реализовать через генераторы (yield)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment