I have a razor page with the following code:
@code
{
[Inject]
private IJSRuntime jsRuntime{ get; set; }
private string jsonString {get;set;}
protected override async void OnInitialized()
{
string json = await jsRuntime.InvokeAsync<string>("initializeElkJs", jsonString);
}
[JSInvokable]
public static Task DrawGraph(string json)
{
int a = 0;
return null;
}
}
And a JavaScript function as the following:
window.initializeElkJs = function (jsonData)
{
const graph = JSON.parse(jsonData);
const elk = new ELK();
const layoutOptions = {
'elk.algorithm': 'layered'
};
let result = "";
elk.layout(graph, layoutOptions)
.then((g) =>
{
//return JSON.stringify(g, null, " "); //Did not work
result = JSON.stringify(g, null, " "); //Did not work
//DotNet.invokeMethodAsync('BlazorServerTest', 'DrawGraph', JSON.stringify(g, null, " ")); //Did not work
});
return result; //Did not work
}
I need to get the result string back to the razor page so I could process it in it, but as you can see, none of the method I tried worked, I later find out that return
in the then
closure returns another Promise
, I have also tried using await
in JavaScript, but unfortunately that returns yet another Promise
, and I can not find any way to “get out of the Promise
“.
Could somebody please be so kind teaching me how to properly get the return string so I could process it in the razor page?
Thank you very much for your help!
PS. This is the elkjs library if needed, all you need is elk.bundled.js
.