65.9K
CodeProject is changing. Read more.
Home

Using Q Promise Library to Handle Async Functions on Array Contexts

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Nov 6, 2013

CPOL
viewsIcon

6991

Using Q Promise Library to Handle Async Functions on Array Contexts

No more JQuery Deferred for me. From now 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...

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

To execute 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. Check this jsFiddle.

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