Skip to content

Advanced Async JavaScript Binding (JSB)

Alex Maitland edited this page Jan 29, 2020 · 8 revisions

Work In progress, more coming soon!

Before starting here please make sure you've read https://github.com/cefsharp/CefSharp/wiki/General-Usage#async-javascript-binding-jsb as it's important you understand the fundamentals of how to bind an object as they are not covered in this article.

Javascript Callback

  • Can Execute at a later point in time
  • Can return a result
  • Can pass in simply structs/classes as param
  • Takes param array so multiple arguments can be passed

A IJavascriptCallback is a proxy for a method in Javascript. You can call a bound method and pass in a reference to a function, then at some later point in your .Net code you can execute the IJavascriptCallback.ExecuteAsync method and the Javascript function will be called.

public void TestCallback(IJavascriptCallback javascriptCallback)
{
	const int taskDelay = 1500;

	Task.Run(async () =>
	{
		await Task.Delay(taskDelay);

		using (javascriptCallback)
		{
			await javascriptCallback.ExecuteAsync("This callback from C# was delayed " + taskDelay + "ms");
		}
	});
}
function MyCallback(string msg)
{
	alert("My callback returned - " + msg):
}

boundAsync.testCallback(MyCallback);
Clone this wiki locally