Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A cleaner alternative to what @Ivo has suggested would be an <strong>Asynchronous Method Queue</strong>, assuming that you only need to make one async call for the collection.</p> <p>(See <a href="http://www.dustindiaz.com/async-method-queues/" rel="noreferrer">this post</a> by Dustin Diaz for a more detailed explanation)</p> <pre><code>function Queue() { this._methods = []; this._response = null; this._flushed = false; } (function(Q){ Q.add = function (fn) { if (this._flushed) fn(this._response); else this._methods.push(fn); } Q.flush = function (response) { if (this._flushed) return; this._response = response; while (this._methods[0]) { this._methods.shift()(response); } this._flushed = true; } })(Queue.prototype); </code></pre> <p>You simply create a new instance of <code>Queue</code>, add the callbacks you need, and then flush the queue with the async response.</p> <pre><code>var queue = new Queue(); queue.add(function(results){ for (var result in results) { // normal loop operation here } }); someFunction(param1, param2, function(results) { queue.flush(results); } </code></pre> <p>An added benefit of this pattern is that you can add multiple functions to the queue instead of just one.</p> <p>If you have an object which contains iterator functions, you can add support for this queue behind the scenes and write code which looks synchronous, but isn't:</p> <pre><code>MyClass.each(function(result){ ... }) </code></pre> <p>simply write <code>each</code> to put the anonymous function into the queue instead of executing it immediately, and then flush the queue when your async call is complete. This is a very simple and powerful design pattern.</p> <p>P.S. If you're using jQuery, you already have an async method queue at your disposal called <a href="http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/" rel="noreferrer">jQuery.Deferred</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload