How can I open next tab upon closing existing tab? The main thing here is I have a continuasly reloading pages in executingFunctions
and as soon as one of the pages has proper html, puppeteer starts to interact with it, closes it and opens a new one. But what if I don’t want this page to be interacted and I want to close it.
concurrentOrders.js
export default async function concurrentOrders(limit, links) {
const executingFunctions = [];
let currentIndex = 0;
while (currentIndex < links.length) {
while (executingFunctions.length < limit && currentIndex < links.length) {
const link = links[currentIndex];
currentIndex++;
const promise = createBuyOrderPromise(...link).finally(() => {
const index = executingFunctions.indexOf(promise);
if (index !== -1) executingFunctions .splice(index, 1);
});
executingFunctions.push(promise);
}
await Promise.race(executingFunctions);
}
}
createBuyOrderPromise.js
export default async function createBuyOrderPromise(link) {
return new Promise(async (resolve, reject) => {
try {
const page = await makeConnection(); // launch in existing browser using puppeteer (via webSocketUrl)
await gotoMarketItemPage(page, link);
await closeTabPage(page); // closing tab
resolve();
} catch () {
reject();
}
});
}
gotoMarketItemPage.js
export default async function gotoMarketItemPage(page) {
try {
await page.goto(page,
{
waitUntil: "domcontentloaded",
timeout: 0,
}
);
const image = await page.$(itemImageSelector);
if (image) elementFound = true;
if (!image) await page.reload();
}
}
Tried to implement puppeteer’s
await page.on("close", () => {
reject();
});
with passing reject
function to gotoMarketItemPage
in gotoMarketItemPage.js
but it made it work like:
if I close one of html loaded pages without the content that I need, this action stops reloding other pages in process in addition to that it stops adding up a page to the stack.
P.S. That is not full version of code for the sake of understanding. Sorry for bad explanation. Let me know if I can clarify this.