[ 8 posts ]

Julien Colomb

  • Username: goldopuk
  • Joined: Tue Apr 03, 2012 4:37 am
  • Posts: 4
  • GitHub: goldopuk
  • Gists: goldopuk
  • Offline
  • Profile

How to synchronize asynchronous tasks in YUI ?

Post Posted: Fri Jul 06, 2012 11:16 am
+0-
I am working with PhoneGap and YUI for a while now and I have to synchronize asynchronous tasks all the time as I am using the sqlite local DB and XHR request when online.
All the SQL and XHR requests are asynchronous.

Often, I want to do a few asynchronous tasks and based on the result of those requests, I will do something.

I am using a lot the AsyncQueue to make synchrone what is NOT.

I developed recently another phonegap+jquery app and I discover the Defered class which allow to synchronize Asynchronous task, and execute some stuff as a result.

In YUI, what is the best way to synchronize asynchronous tasks ?

I hope my question est clear enough. If not, let me know.

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile
Tags:

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Fri Jul 06, 2012 11:21 am
+0-
Javascript, by nature, is async and should be treated that way in the majority of the use cases.

You could simplify things with Y.Parallel:

http://yuilibrary.com/yui/docs/yui/parallel.html

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 620
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Fri Jul 06, 2012 5:10 pm
+0-
Or you can use the Deferred Gallery module: http://yuilibrary.com/gallery/show/deferred which is pretty similar to jQuery's because it implements the same CommonJS standard.

FastCars

  • Username: FastCars
  • Joined: Thu May 10, 2012 1:03 pm
  • Posts: 5
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Fri Jul 06, 2012 5:33 pm
+0-
Parallel shows;
yui-base (33.57 KB)
parallel (0.54 KB)

Just running Deferred Gallery with io-base uses some of these classes;
yui-base (33.57 KB)
oop (2.28 KB)
event-custom-base (11.83 KB)
querystring-stringify-simple (0.39 KB)
io-base (5.23 KB)

I'm assuming that parallel is going to run faster assuming your asynchronous calls are not using io-base, but your Deferred Gallery program seems to allow predefined events relating to success, or failure. That's pretty cool, might be useful if you need to check if the call completed correctly.

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 620
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Sat Jul 07, 2012 6:43 am
+0-
The only requirement that Deferred has is 'oop'. I'll have to include it in the module requirements because I forgot about 'yui-base'. The rest are requirements for io-base.

Parallel is useful in its simplicity. Deferreds do more stuff in a more contrived way, but they allow you to write crazy stuff like:
Code:
var node = Y.one('#foo').plug(Y.Plugin.NodeDeferred);
node.deferred.once('click').hide(true).setStyle('display', 'none');

Which otherwise you would've had to write as:
Code:
var node = Y.one('#foo');
node.once('click', function () {
  node.hide(true, function () {
    node.setStyle('display', 'none');
  });
});

Gallery Deferred is 5kb large. If there's enough interest I can divide it into different modules. That's something I'll probably do in the future, when Dav makes the Gallery more like the YUI Core.

Steven Olmsted

YUI Contributor

  • Username: solmsted
  • Joined: Wed Apr 14, 2010 1:47 pm
  • Posts: 82
  • Location: Richmond, VA
  • GitHub: solmsted
  • Gists: solmsted
  • IRC: solmsted
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Mon Jul 09, 2012 10:12 am
+0-
Another possibility is gallery-async and gallery-async-command.
http://yuilibrary.com/gallery/show/async
http://yuilibrary.com/gallery/show/async-command

Y.Async was created to solve exactly this problem but it's almost completely backwards compared to deferred. Where deferred wraps the result of an asynchronous task in an object, Y.AsyncCommand wraps the asynchronous task itself in an object and fires events as its status changes. Y.Async is a convenient helper for stacking and nesting many tasks together at once.

I haven't done any real benchmarks comparing these solutions. If performance is your top priority, I would guess that Y.Parallel is best and Y.Async is worst but that's a complete guess. I believe that Y.Async is still worth it for all of the convenience it provides at development time, and I've been using it heavily in some pretty demanding node js servers and I've been quite happy with it so far.

Julien Colomb

  • Username: goldopuk
  • Joined: Tue Apr 03, 2012 4:37 am
  • Posts: 4
  • GitHub: goldopuk
  • Gists: goldopuk
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Tue Jul 24, 2012 5:33 am
+0-
Thanks a lot for all those informations.

I am sorry, I am just seeing right know all those answers. The emails coming from YUI where lost in my mailbox in some obscur corner.

I am going to study the different choices and possibilities beside AsyncQueue.

There are very few discussions on that subject on the web but also in BOOKS. I did not find any books discussing about asynchronous javascript and the common problems we are facing as developers and the patterns to solve them.
I'd like to write it, but lack of experiences :)

Julien Colomb

  • Username: goldopuk
  • Joined: Tue Apr 03, 2012 4:37 am
  • Posts: 4
  • GitHub: goldopuk
  • Gists: goldopuk
  • Offline
  • Profile

Re: How to synchronize asynchronous tasks in YUI ?

Post Posted: Fri Aug 03, 2012 5:55 am
+0-
I discover a new book about nodejs in which a chapter is dedicated to asynchronous javascript patterns. "Learning node" by Shelleys Powers. I did not yet read it in details but i like it already.

http://bit.ly/PrH04z
  [ 8 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum