edge.js using node module

250 Views Asked by At

I'm trying the following with edge.js and Visual Studio:

class Program
{
    public static async void Start()
    {
        var data = 9;
        var func = Edge.Func(File.ReadAllText("do_some_math.js"));
        Console.WriteLine(await func(data));
    }

    static void Main(string[] args)
    {
        Task.Run((Action)Start).Wait();
    }
}

do_some_math.js:

return function (data, callback) {

    var math = require('mathjs');    

    // console.log("result: " + math.sqrt(data));  // works
    callback(null, "result: " + math.sqrt(data));  

}

I can't get a result when using callback, while mathjs works when using console.log. It seems the callback does not work after require('mathjs'). I tryed also with other modules. What's wrong here?

1

There are 1 best solutions below

0
On

Many thanks to edge.js, they provided the answer. I guess this should have been a question about C#. They answered:

The reason you are not getting a result is that the .NET application exits before the Node.js code has a chance to complete. You can verify it by putting System.Threading.Sleep(5000); as the last statement in Main.

The way to address it properly (i.e. without Sleep) is to modify the signature of Start to

public static async Task Start()

And then in Main call it with:

Start().Wait();