Last active
February 13, 2024 11:53
-
-
Save nikpros/0fb75e7bf84e7bd47155659061e1270b to your computer and use it in GitHub Desktop.
test task by Yandex/VK tech
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
}); | |
} | |
} |
есть подозрение, что 4 требование не выполняется, боюсь, что 23-24 строчки не особо этому способствуют :)
P.S. может стоит попробовать реализовать через генераторы (yield)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Им нужна была функция с 3-мя параметрами: массив url, лимит одновременных вызовов и колбек по завершению оных.
Требования: