function fn(id){
return new Promise((resolve, reject) => {
fs.readFile(параметры, (data)=>{
resolve(data);
});
});
}
Promise.all((()=>{
let resP = [];
for(let i = 0; i < 1000; i++){
resP[i] = fn(i)
.then() //если нужно
.catch() //если нужно
;
}
return resP;
})()).then(()=>{
//Все промисы отработали как надо
}).catch()// или как не надо...
;
(()=>{
const min = 1;
const max = +document.getElementsByClassName('navigation')[0].lastElementChild.innerHTML;
const bestComm = document.getElementsByClassName('comm_best')[0].children;
let news_id = window.location.pathname.match(/\d*-/)[0].match(/\d*/)[0];
let _cache = [];
let getComments = (cstart, news_id) => {
return new Promise((responsed) => {
if (!_cache[cstart]){
$.get(dle_root+"engine/ajax/comments.php", {cstart:cstart,news_id:news_id,skin:dle_skin}, (data) => {
_cache[cstart] = data.comments;
responsed(data.comments);
},"json");
} else {
responsed(_cache[cstart]);
}
});
};
let binarySearch = (id, n1, n2) => {
return (Promise.all([getComments(n1, news_id), getComments(n2, news_id), getComments(Math.ceil((n1+n2)/2), news_id)])
.then((data) => {
if(data[0].search(new RegExp(`${id}\\D`)) !== -1){
return n1;
}
if(data[1].search(new RegExp(`${id}\\D`)) !== -1){
return n2;
}
if(data[2].search(new RegExp(`${id}\\D`)) !== -1){
return Math.ceil((n1+n2)/2);
}
if(id.match(/\d+/)[0] < data[2].match(/comment-id-\d+/g)[0].match(/\d+/)[0]){
return binarySearch(id, n1+1, Math.ceil((n1+n2)/2));
} else {
return binarySearch(id, Math.ceil((n1+n2)/2), n2-1);
}
}).catch((err) => {
return Promise.reject(err);
}));
};
for (let i = 0; i < bestComm.length; ++i) {
binarySearch(bestComm[i].id, min, max-1).then(
(page)=>{
bestComm[i].onclick = ()=>{CommentsPage(page, news_id); return false;};
console.log(`Page: ${page}`);
},
(err)=>{
console.error(err);
});
}
})();