EvaulateScriptAsPromiseAsync 和 async/await
EvaulateScriptAsPromiseAsync and async/await
我正在尝试将 EvaluateScriptAsPromiseAsync
与 CefSharp 一起使用。
它似乎在浏览器中工作,但我在 cefSharp 中得到一个空结果。
Javascript:
(async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();
CSharp 代码:
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
完整代码:
public partial class Form1 : Form
{
ChromiumWebBrowser browser;
public Form1()
{
InitializeComponent();
browser = new ChromiumWebBrowser("jquery.com");
this.Controls.Add(browser);
browser.FrameLoadEnd += Browser_FrameLoadEnd;
}
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
const string script = @"(async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();";
if (e.Frame.IsMain)
{
// Get us off the main thread
Task task = new Task(async () =>
{
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
});
task.Start();
}
}
}
所以上面示例中的 IIFE
计算为 Promise
,所需的额外步骤实际上是 return 承诺。传递给 EvaluateScriptAsPromiseAsync
的脚本
必须 return 一个值,否则你会得到空值。
Differs from other EvaluateScriptAsync methods in that you must return a result.
//This will return 42
await browser.EvaluateScriptAsPromiseAsync("return 42");
//This will return undefined
await browser.EvaluateScriptAsPromiseAsync("42");
根据https://github.com/cefsharp/CefSharp/pull/3251
因为它包含在 IIFE
中,然后传递给 Promise.resolve
,您需要 return 结果。
例如
return (async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
const string script = @"return (async function() { const result = await $.ajax({ type: 'GET', url: './robots.txt' }); return result; })();";
if (e.Frame.IsMain)
{
// Use the Thread Pool
Task.Run(async () =>
{
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
});
}
}
我正在尝试将 EvaluateScriptAsPromiseAsync
与 CefSharp 一起使用。
它似乎在浏览器中工作,但我在 cefSharp 中得到一个空结果。
Javascript:
(async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();
CSharp 代码:
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
完整代码:
public partial class Form1 : Form
{
ChromiumWebBrowser browser;
public Form1()
{
InitializeComponent();
browser = new ChromiumWebBrowser("jquery.com");
this.Controls.Add(browser);
browser.FrameLoadEnd += Browser_FrameLoadEnd;
}
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
const string script = @"(async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();";
if (e.Frame.IsMain)
{
// Get us off the main thread
Task task = new Task(async () =>
{
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
});
task.Start();
}
}
}
所以上面示例中的 IIFE
计算为 Promise
,所需的额外步骤实际上是 return 承诺。传递给 EvaluateScriptAsPromiseAsync
的脚本
必须 return 一个值,否则你会得到空值。
Differs from other EvaluateScriptAsync methods in that you must return a result.
//This will return 42
await browser.EvaluateScriptAsPromiseAsync("return 42");
//This will return undefined
await browser.EvaluateScriptAsPromiseAsync("42");
根据https://github.com/cefsharp/CefSharp/pull/3251
因为它包含在 IIFE
中,然后传递给 Promise.resolve
,您需要 return 结果。
例如
return (async function() {
const result = await $.ajax({ type: 'GET', url: './robots.txt' });
return result;
})();
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
const string script = @"return (async function() { const result = await $.ajax({ type: 'GET', url: './robots.txt' }); return result; })();";
if (e.Frame.IsMain)
{
// Use the Thread Pool
Task.Run(async () =>
{
var result = await browser.EvaluateScriptAsPromiseAsync(script);
Debug.Assert(result.Result != null);
});
}
}