daGrevis
daGrevis

Reputation: 21333

phantom.exit(-1) doesn't work when called from another function

I'm using PhantomJS to run JavaScript. There's a function phantom.exit() that accepts exit code as 1st argument.

Here's how it works...

var page = require("webpage").create(),
    $ = require("jquery")

page.open("http://127.0.0.1:8081/", function (status) {
    var title = page.evaluate(function() {
        return $("title").text()
    })
    phantom.exit(-1)
})

Returned exit code is -1 or 255 as expected.

My problem is that I need to exit script when my function is called. My function contains phantom.exit(-1).

var page = require("webpage").create(),
    $ = require("jquery")

function foo() {
    phantom.exit(-1)
}

page.open("http://127.0.0.1:8081/", function (status) {
    var title = page.evaluate(function() {
        return $("title").text()
    })
    foo()
    phantom.exit()
})

Problem is that phantom.exit(-1) that's in foo() function is not called and exit code is 0.

There's a workaround to check for return value in my function and call phantom.exit() manually, but it's not as I want it... Any help is much appreciated!

Upvotes: 0

Views: 1829

Answers (1)

tomsseisums
tomsseisums

Reputation: 13367

There is a possibility, that your foo function call to phantom.exit output got overriden by the page.open callbacks one.

Hence, calling foo() set exit code to -1.
Afterwards, calling phantom.exit without parameter, set the exit code back to 0.

Because phantom itself, probably, returns the exit code after everything has been executed, you got 0, with no signs of -1.

Upvotes: 3

Related Questions