is it possible to get the outerHTML without then() method?
I can't output the outerHTML.
There is a delay because of promise, and the return(a) happened earlier than the console.log(a); I don't want to use SetTimeOut().
Here is my code...
function(){
var a;
Runtime.evaluate({expression:'document.documentElement.outerHTML'}).then((result)=>{
a = result.result.value;
**console.log(a);**
});
**return a;**
}
It's not a matter of using a promise here, all the methods of the protocol are inherently asynchronous so you must use one of the common JavaScript patterns used to serialize the execution of your asynchronous code. In fact you could have used the callback API in your code (still wrong):
function f() {
var a;
Runtime.evaluate({expression: 'document.documentElement.outerHTML'}, (error, result) => {
a = result.result.value;
console.log(a);
});
return a;
}
function f(callback) {
Runtime.evaluate({expression: 'document.documentElement.outerHTML'}).then((result) => {
const a = result.result.value;
callback(null, a);
}).catch((err) => {
callback(err);
});
}
// caller
f((err, result) => {
if (err) {
console.error(err);
} else {
console.log(result);
}
});
function f() {
return Runtime.evaluate({expression: 'document.documentElement.outerHTML'}).then((result) => {
const a = result.result.value;
return a;
});
}
// caller
f().then((a) => {
console.log(a);
}).catch((err) => {
console.error(err);
});
// same f() as above
async function caller() {
try {
const a = await f();
console.log(a);
} catch (err) {
console.error(err);
}
}
caller();
Most helpful comment
It's not a matter of using a promise here, all the methods of the protocol are inherently asynchronous so you must use one of the common JavaScript patterns used to serialize the execution of your asynchronous code. In fact you could have used the callback API in your code (still wrong):
Callbacks
Promises
Promises (with async/await)