The script below outputs only the value of variable "result2"
How to output the value of "result1" and "result2" without using console.log, cuz i will put this script in the cefSharp framework of C#, cefSharp does not output data that is in "console.log"
var result;
var result2;
const myPromise = new Promise(
function(resolve)
{
var a1 = 2 + 2;
resolve(a1);
}
);
myPromise
.then(res=>{return result = res;});
result; //output=4
myPromise
.then(res2=>{result = res2+1; return result2=result;});
result2; //output=5
var result;
var result2;
const myPromise = new Promise(
function(resolve)
{
var a1 = 2 + 2;
resolve(a1);
}
);
myPromise
.then(res=>{return result = res;});
result;
myPromise
.then(res2=>{result = res2+1; return result2=result;});
result2;
Here is the info about execute JavaScript and Promise in cefSharp
Evaluate Javascript in the context of this Browsers Main Frame. The script will be executed asynchronously and the method returns a Task encapsulating the response from the Javascript. The result of the script execution in javascript is Promise.resolve so even no promise values will be treated as a promise. Your javascript should return a value. The javascript will be wrapped in an Immediately Invoked Function Expression. When the promise either trigger then/catch this returned Task will be completed.
var script = "return new Promise(function(resolve, reject) { setTimeout(resolve.bind(null, { a: 'CefSharp', b: 42, }), 1000); });"
JavascriptResponse javascriptResponse = await browser.EvaluateScriptAsPromiseAsync(script);