Answer the question
In order to leave comments, you need to log in
PhantomJS - how to open multiple pages for a loop?
Hello.
Because .open is asynchronous in PhantomJS, the loop doesn't wait for a response and moves on. There are solutions with setTimeout, but they do not suit me, because of the fixed delay. I also have an option to run a script sending a request separately, and just wait for its response, exec is not asynchronous (I hope).
page.open(platform.url.list.replace('{PAGE.NUM}', numPage), function(sList) {
if(sList === "success") {
var listResources = page.evaluate(handler.getListResources);
for(iList in listResources) {
page.open(platform.url.list.replace('{RES.ID}', listResources[iList].id), function() {
console.log(1);
if(sVersions === "success") {
var versionsResource = page.evaluate(handler.getVersions);
for(iVersion in versionsResource) {
listResources[iList].versions = versionsResource[iVersion];
console.log(5)
}
}
});
};
console.log(JSON.stringify(listResources));
numPage++;
} else phantom.exit();
});
for(iList in listResources), everything is executed 1 time, the last one is displayed
console log(1);1 time, although there are 20 links.
Answer the question
In order to leave comments, you need to log in
try ...
page.open(platform.url.list.replace('{PAGE.NUM}', numPage), async function(sList) {
and then
var listResources = await page.evaluate(handler.getListResources);
.. for( iList
in listResources) {
await page.open(platform.url.list.replace('{RES.ID}', listResources[iList].id), function() {
will something change?
somewhere for sure it is possible to force JS to "wait"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question