A phantomjs-node extension that make phantom page support promise.
import 'phantom-page-promise';
import phantom from 'phantom';
const init = async () => {
const ph = await phantom.create();
const page = await ph.createPage();
const status = await page.open('http://facebook.com');
if (status.toLowerCase() !== 'success') { throw new Error(status); }
const result = await page.evaluatePromise(
`function () {
return new Promise(function (resolve) {
setTimeout(function () {
resolve('it works');
}, 2000);
})
}`
);
console.log(result); // 'it works'
ph.exit();
page.close();
};
init();
Just like page.evaluateJavaScript
, but could evaluate and return a promise.
codeOrFunction
(String|Function): Evaluate a function or function contained in a string.options
(Object): See below for detail.
timeout
(Number): Specify timeout of execution. If timeout before the promise return, it would throw aTIMEOUT
error. Defaults to 10000(ms).delay
(Number): Specify delay of execution. Defaults to 0(ms).args
([Any]): Specify arguments to the function. Only work if the type ofcodeOrFunction
isFunction
.
(Promise): The result.
Using npm:
npm install --save phantom-page-promise
Using yarn:
yarn add phantom-page-promise
MIT