I am familiar with asynchronous programming and using callbacks, however I am not as knowledgeable in using promises. I have the following code:
page.evaluate(function() {
elements.push(document.getElementById('form_username'));
elements.push(document.getElementById('form_password'));
return elements;
}).then(function(html){
console.log(html[0].className);
}).then( () => {
_page.close();
_ph.exit();
}).catch(err => console.log(err));
I am getting an error saying that 0 doesnt exist for type undefined when I pass it to 'html' and try to get the className, and I believe that is the case because when I return elements it doesnt wait the HTML elements to get pushed in because it is Asynchronous. I know that I can use callback(elements) if I were using callbacks, but I am not sure what to do in this case.
EDIT: page.evaluate() is a function used by phantomjs-node, found here https://github.com/amir20/phantomjs-node. Also this is just a piece of the code. When returning just a single HTML element I am able to get its className. Like this:
page.evaluate(function() {
return document.getElementById('form_username');
}).then(function(html){
console.log(html.className);
}).then( () => {
_page.close();
_ph.exit();
}).catch(err => console.log(err));