| Page 1 of 1 | [ 8 posts ] |
|
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. |
|
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 DopazoYUI Contributor
|
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.
|
|
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 DopazoYUI Contributor
|
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. |
|
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. |
|
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 |
|
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 |
| Page 1 of 1 | [ 8 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group