Start a new topic

using async, await or tasks in general

Hey there,

can you give a short example on how to deal with async methods?

I want two create a binding for simple async methods like ajax requests.

I can't find a way to create a new task..


there is  no Task.Run(..)

no semaphores..


this is what i have / want ;)

// the binding of a async java script function
[Js(Name = "Call")]
private extern void Call(object options, Action<Data> successCallback, Action<Error> errorCallback);

// the method i want for use in C#
puublic async Task<Data>CallAsync(object options)
{
    // call the extern method
    // wait for one of the callbacks
    // return the value of Data in case of success
    // return null in case of an error
}

   thanks!


In the features section, there is no "partial" in front of "support for async and await" any more. However, there is no sample on how to use them in duocode and using the live demo I was not able to create a working sample my own... I can not see a Run(..) method in the Task class.


Please show us how to use this feature! (or remove it from the feature list!)


Thank you!

Thanks for reporting, async / await support is still partial and not stable yet, especially the Task counterpart in JS.

Here we have discussions to combine JS Promise and Task support and of course improve async support.

See: http://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise


Such an improvements are in 2.5 road-map (couple months from now).


We do have extensive WebWorkers support from DuoCode 1.3 (Nov 2015) with sample code see `WebWorkerTests.cs` in `UnitTest` template.


Again thanks for reporting and we very appreciate your feedback,

Daniel

I'm currently working with jquerys deferred objects which looks very similar. However, this also results in a lot auf callbacks or lambdas which is very ugly to read.. and write.. 

I'm looking forward to see a real async await mechanism.. maybe you can offer a preview build at some point ;)


I'll also look into web workers!


Thanks!

You could this:

 // the binding of a async java script function

        [Js(Name = "Call", OmitGenericArgs = true)]

        public static extern void Call<T>(object options, Action<T> successCallback, Action<Exception> errorCallback);

        public static Task<T> CallAsync<T>(object options = null)

 {

            var tasker = new TaskCompletionSource<T>();

            Action<T> success = new Action<T>((T result) => tasker.SetResult(result));

            Action<Exception> failure = new Action<Exception>((Exception error) => tasker.SetException(error));

            Root.Call<T>(options, success, failure);

            return tasker.Task;

 }


Then use like any other task:

          object obj = ...;

           CallAsync<string>(obj).ContinueWith((Task<string> result) => {

                System.Console.WriteLine(result);

 });


UNTIL THEY GET ASYNC/AWAIT working, then you could


object obj = ...;

string data = await CallAsync<string>(obj);

Basically using Task is moe Syntax Sugar so you can use:


Task<string> result = CallAsync<string>(obj);


then do what ever on the task... result.Wait() or result.ContinueWith()


and in FUTURE:


string data = await CallAsync<string>(obj);

Note: This is how i WRAP all my "Async Functions" that take a callback inside a native C# Async Task API...


That way it feels more like .NET then Javascript Function Callbacks

Hi,

after copying your post to visual studio, sorting and trying to understand what you do i think i got it ;)

But eventually, the only thing you can do is replace the second line of the follwing snipped by the third one, right? 

var obj = new object();
Call<string>(obj, (result) => System.Console.WriteLine(result), (error)=> {});
CallAsync<string>(obj).ContinueWith((result) => System.Console.WriteLine(result));

I can't see how this is helping (except the fact, that later, when async/await is supported, i need to do a little less refactoring ...


the thing async/await is really required for is to NOT wrap things in callbacks (Actions) like here:

var handle = CallAsync<string>(obj);

// start some other tasks or whatever 
// ...

var res = await handle; 
System.Console.WriteLine(res);

 

Yep... I also am waiting for the await function to really work. So for now is just a little syntax sugar so your code is using c# Task pattern that is also compatible in regular native application.... I mainly use the task wrapped API for RPC calls from my View Model code... I can then use that same exact view model code for my business logic in both DuoCode html projects as well as native projects It will be even sweeter when they get the await keyword working.
Also... No matter what it is still a JavaScript callback that uses A function as a callback... Even when they get await going... You still would have to WRAP the callbacks in a TaskCompletionSource in the backend to be able to use the Task API sugar... Your actual application code that calls CallAsync will be cleaner .... But at some point, there will ALWAYS be some wrapped code on the backend
Login or Signup to post a comment