Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Perhaps you code is not well designed?</h1> <p>I do not see how that would be an issue. The asynchronous process should be responsible for creating a new <code>Deferred</code> object everytime.</p> <pre><code>function doSomething() { var d = $.Deferred(); setTimeout(function () { d.resolve(); }, 1000); return d; } function doSomethingElse() { var d = $.Deferred(); setTimeout(function () { d.resolve(); }, 1000); return d; } </code></pre> <p>Then you can always do the following:</p> <pre><code>$.when(doSomething(), doSomethingElse()).done(function () { console.log('done'); }); </code></pre> <h1>There's always a solution:</h1> <p>If you absolutely need to be able to call <code>resolve</code> multiple times on the same <code>Deferred</code>, then you should wrap the <code>Deferred</code> into another object, let's say <code>DeferredWrapper</code>, which would expose the same API as a <code>Deferred</code> but would delegate all method calls to the it's encapsulated <code>Deferred</code>.</p> <p>In addition of delegating the function calls, the <code>DeferredWrapper</code> would have to keep track of all listening operations (e.g. done, always, fail...) that were made on the object. The <code>DeferredWrapper</code> could store all actions as <em>[functionName, arguments]</em> tuples in an internal <code>this._actions</code> property.</p> <p>Finally, you would need to provide a special implementation for state changing operations (e.g. reject, resolve, resolveWith...etc) that would look like:</p> <ol> <li><p>Let <em>d</em> be the internal <code>Deferred</code> referenced by <code>this._deferred</code>.</p></li> <li><p>Let <em>fn</em> be the function name of the function being called.</p></li> <li><p>If <code>d.state()</code> is not <em>pending</em>:</p> <p>3.1 Do <code>d = this._deferred = [[native jQuery Deferred]]</code></p> <p>3.2 Apply all actions on <code>d</code>.</p></li> <li><p>Return the result of <code>d[fn].apply(d, arguments)</code></p></li> </ol> <p><em>Note: You would also need to implement a custom <code>promise</code> implementation and make sure it behaves correctly. You can probably use a similar approach like the one described.</em></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. 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