Answer the question
In order to leave comments, you need to log in
How to make the execution of functions in turn?
A bit of history about how the script works...
When I run the script, it happens that the status function is executed and the value is immediately returned undefined, after which this value is returned return status(); after page.open(url, status); loaded callback returns the value success, but this value does not get into return status(); it is still undefined there.
The question for such js connoisseurs is how to do what after loading the page using page.open(url, status); get the success or fail status and give it at the output of the xPage(' https://ya.ru/ ') function, boolean value True or False.
var webPage = require('webpage'),
page = webPage.create();
function xPage(url){
var xStatus = function(status, a) {
if(status === 'success')
{
console.log('StatusPage: ' + status);
a = 'ggggggggggggg';
return{a:a}
}
else
if(status === 'fail')
console.log('StatusPage: ' + status);
else
console.log('Status = undefined');
}
var b;
page.open(url, xStatus(b));
console.log(b);
}
xPage('https://ya.ru/');
Answer the question
In order to leave comments, you need to log in
How can I make a function run only after another one has completed?
Using classic promises , it would look something like this
var webPage = require('webpage'),
page = webPage.create();
var Promise = require('bluebird');
function xPage(url){
return new Promise(function (resolve, reject) {
page.open(url, function (status) {
if (status !== 'success') {
console.log('StatusPage: ' + status);
reject(status);
} else {
console.log('StatusPage: ' + status);
var a = 'ggggggggggggg';
resolve({
a: a
});
}
});
});
}
xPage('https://ya.ru/')
.then(function (stat) {
console.log( JSON.stringify(stat) );
page.render(Date.now() + '.png');
phantom.exit();
})
.catch(function(err){ console.log(err) })
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question