B
B
babbert2019-03-08 08:10:36
JavaScript
babbert, 2019-03-08 08:10:36

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();
    });

In a loop
for(iList in listResources)
, everything is executed 1 time, the last one is displayed
console log(1);
1 time, although there are 20 links.
Is it possible to somehow pause the loop until the callback function unpauses it and the loop continues?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-03-08
@babbert

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 question

Ask a Question

731 491 924 answers to any question