Пример #1
0
        private static async Task CreatePromiseAsync(IJsObject window)
        {
            //It is also possible to create a wrapper class for IJsObject that simplifies appending the
            //handlers and type checks. Such approach can be used to integrate JavaScript promises
            //with async/await in the .NET application.

            //Create a promise that is fulfilled and wrap this promise
            Console.WriteLine("\nCreate another promise that is fulfilled...");
            JsPromise promise3 = window.Invoke <IJsObject>("CreatePromise", true).AsPromise();

            JsPromise.Result result = await promise3.Then(o =>
            {
                Console.WriteLine($"Callback:Success: {o}");
                return(o);
            })
                                      .ResolveAsync();

            Console.WriteLine($"Result state:{result?.State}");
            Console.WriteLine($"Result type:{(result?.Data?.GetType().ToString() ?? "null")}");

            //Create a promise that is rejected and wrap this promise
            Console.WriteLine("\nCreate another promise that is rejected...");
            JsPromise promise4 = window.Invoke <IJsObject>("CreatePromise", false).AsPromise();

            result = await promise4.ResolveAsync();

            Console.WriteLine($"Result state:{result?.State}");
            Console.WriteLine($"Result type:{(result?.Data?.GetType().ToString() ?? "null")}");
        }
Пример #2
0
 public static JsPromise AsPromise(this IJsObject jsObject) => JsPromise.AsPromise(jsObject);