Chrome-remote-interface: How to get value by Runtime. evaluate without then()

Created on 21 Apr 2017  路  1Comment  路  Source: cyrus-and/chrome-remote-interface

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;** 
}
question

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):

function f() {
    var a;
    Runtime.evaluate({expression: 'document.documentElement.outerHTML'}, (error, result) => {
        a = result.result.value;
        console.log(a);
    });
    return a;
}

Callbacks

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);
    }
});

Promises

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);
});

Promises (with async/await)

// same f() as above

async function caller() {
    try {
        const a = await f();
        console.log(a);
    } catch (err) {
        console.error(err);
    }
}

caller();

>All comments

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;
}

Callbacks

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);
    }
});

Promises

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);
});

Promises (with async/await)

// same f() as above

async function caller() {
    try {
        const a = await f();
        console.log(a);
    } catch (err) {
        console.error(err);
    }
}

caller();
Was this page helpful?
0 / 5 - 0 ratings