-1

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"

enter image description here

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);
  • Question is not clear. What are you trying to achieve here? If only want to print, why you don't want to use `console.log` ? – manpreet May 11 '22 at 11:20
  • use `console.log(result, result2)` to log in console. – Shikhar Awasthi May 11 '22 at 11:20
  • *"without using console.log"* - Well, if you want to log something to the console, that seems like the right tool for the job. Why don't you want to use it? What's the underlying goal here? – David May 11 '22 at 11:21
  • @manpreet, about trying to achieve, please take a look the image – Csharp_cowboy May 11 '22 at 11:23
  • @David, i will put this script in the cefSharp framework of C#, cefSharp does not output data that is in "console.log" – Csharp_cowboy May 11 '22 at 11:25
  • @manpreet, i will put this script in the cefSharp framework of C#, cefSharp does not output data that is in "console.log" – Csharp_cowboy May 11 '22 at 11:27
  • 2
    @Csharp_cowboy: Then how does it output data? How is that output affected by the Promises you're using (and why are you even using Promises for synchronous operations)? Focus on the actual problem you're trying to solve. Provide a [mcve] using cefSharp which demonstrates what you are trying to do and what isn't working as expected. Because currently the question makes little sense. – David May 11 '22 at 11:27

1 Answers1

1

The console.log call just displays the value of the previous statement and nothing more.
Using the result outside the promise function can be a problem - the promise might finish after. Maybe want to look into async/await?

Promise.all does it all

I'm not sure about the C# side of things, but as JavaScript goes:
If you want to access both results of the promises you might be interested in implementing Promise.all

const myPromise = new Promise(
    function(resolve)
    {
        let result = 410;
        resolve(result + 10);
    }
);

const myPromise2 = new Promise(
    function(resolve)
    {
        let result2 = 67;
        resolve(result2 + 2);
    }
);

Promise.all([myPromise, myPromise2]).then((values) => {
  // These are all results from all promises
  // You can analyze/modify/copy their results with a forEach or however you like.
  console.log(values);
});

This example implementation can now use all results from all promises.

Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
  • Thank you, but if i want to send value of firstPromise to secondPromise how can i do it? for example to sending the value "420" to secondPromise and making some operation 420+69=489. And the final result of first promise = 420. secondPromise=489. How to do it? – Csharp_cowboy May 11 '22 at 12:34
  • You can find that in StackOverflow already: [How to pass parameter to a promise function](https://stackoverflow.com/questions/35318442/how-to-pass-parameter-to-a-promise-function) – Peter Krebs May 11 '22 at 12:52
  • please help me with this question https://stackoverflow.com/questions/72203250/javascript-how-to-execute-firstpromise-before-secondpromise/72203286#72203286 – Csharp_cowboy May 11 '22 at 15:01
  • 1
    Using async/await is an excellent suggestion. See https://stackoverflow.com/a/65262282/4583726 for a very simple CefSharp example. – amaitland May 11 '22 at 19:50