61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
const PromiseElement = require("./PromiseElement");
|
|
|
|
class PromiseElementList extends PromiseElement {
|
|
async getLength() {
|
|
return (await this.getPromise()).length;
|
|
}
|
|
|
|
get(index) {
|
|
return new PromiseElement(this.getPromise().then(res => res[index]));
|
|
}
|
|
|
|
filter(filterFunc) {
|
|
return new PromiseElementList(this.getPromise().then(async res => {
|
|
let results = [];
|
|
res.forEach(elem => {
|
|
results.push(new Promise(res => {
|
|
res(filterFunc(new PromiseElement(elem)));
|
|
}).catch((e) => {
|
|
console.error(e);
|
|
return false
|
|
}));
|
|
});
|
|
results = await Promise.all(results);
|
|
let filteredRes = [];
|
|
|
|
results.forEach((result, i) => {
|
|
if (result) {
|
|
filteredRes.push(res[i]);
|
|
}
|
|
});
|
|
return filteredRes;
|
|
}));
|
|
}
|
|
|
|
async forEach(callback, simultaneously) {
|
|
const length = await this.getLength();
|
|
let promises = []
|
|
for (let i = 0; i < length; i++) {
|
|
const promise = callback(await this.get(i));
|
|
if (simultaneously !== false){
|
|
await promise;
|
|
}
|
|
else {
|
|
promises.push(promise);
|
|
}
|
|
}
|
|
await Promise.all(promises);
|
|
return this;
|
|
|
|
}
|
|
|
|
async click() {
|
|
return this.forEach(e => e.click());
|
|
}
|
|
|
|
filterOne(filterFunc) {
|
|
return this.filter(filterFunc).get(0);
|
|
}
|
|
}
|
|
|
|
module.exports = PromiseElementList; |