Using Q Promise Library to handle async functions on array contexts. #javascript #nodejs

Oscar Brito


No more JQuery Deferred for me. From know on I'm using a Promise Library that allows me more powerful operations like chaining and combinations.

In this post I'm focusing on array operations like:

  1. Execute multiple functions and execute something after all of these have finished
  2. Map() implementation using async functions (aka: qMap)
Each of the points above are available on jsFiddle for you to tweak...

1)


Using Q.all we can achieve this easily. Just take a peak at the following jsFiddle.




2)


To execute a async functions in sequence over an array of items the qMap function was created. The signature of this is the same as the map() but the difference is that each function return a Q.promise to sync the execution.



Unfortunately I could not find any other way, using Q, to achieve this sync execution so I've written the qMap function. If you find any other way to write this please let me know.


LINKS:

http://documentup.com/kriskowal/q/

http://jsfiddle.net/aetheon/qgyey/
http://jsfiddle.net/aetheon/m3pHv/




Visit www.divhide.com for more informations, contacts and news about Web Development.
See other blog posts at blog.divhide.com.



Divhide purpose is to follow the HTML5 movement and contribute with applications which prove the quality of technology.


Feel free to contact divhide.