Why is my deferred object set to resolved instead of pending when calling a function that sets a new


I'm trying to get jQuery <a href="http://api.jquery.com/category/deferred-object/" rel="nofollow">deferred</a> to work and am having some problems.

My system is using postMessage to pass messages from and to sandboxed areas on a website. So when I request a service from a sandboxed area like this:

// on click of button foo.requestService(options, function (response) { $("#c").val(response); // set value of input button }

Internally, I'm creating a new $.deferred every time the button is clicked and the service is requested like so:

that.requestService = $.fn.requestService = function (options, callbackFunction) { var deferred = new $.Deferred(), callbackId = priv.generateUuid(), callback = deferred; // store callback to be retrieved by response handler priv.trackCallback(callbackId, callback, callbackFunction); // set type if (options.type === undefined) { options.type = "request/any"; } // the callback function to be run, once a postMessage // with the response is received deferred.done(function(result, callbackFunction) { if (callbackFunction) { callbackFunction(result); } }); // trigger messaging window.top.postMessage(options, window.location.href); };

All works well, I can request my sandboxed service, run it, postMessage the result and retrieve the respective callback:

priv.returnResult = function (event) { // here I get back the deferred (callback[0]) and callback function (callback[1]) var callback = priv.retrieveCallback(event.data.callback); // problem = the callback stays resolved after the first function call console.log(callback[0].state()); callback[0].resolve(event.data.result, callback[1]); };

My problem is, although I'm creating a **new** $.Deferred every time a service is requested, my deferred object only runs the first time and then is set to resolved, preventing any further function calls to return a result.

I thought a new $.Deferred would generate a new deferred every time a service is requested, which I can resolve to run my callback function.

<strong>Question</strong><br /> What do I need to do to get this to work? Is it not, that <strong>new</strong> creates a <strong>new</strong> object, which I can resolve? Do I have to somehow delete/remove the previous resolved.deferred object in order to make it work?



Found a snippet extending jQuery deferred to handle "multiple deferred setters and resolvers":

$.extend({ StatelessDeferred: function () { var doneList = $.Callbacks("memory"), promise = { done: doneList.add, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function (obj) { var i, keys = ['done', 'promise']; if (obj === undefined) { obj = promise; } else { for (i = 0; i < keys.length; i += 1) { obj[keys[i]] = promise[keys[i]]; } } return obj; } }, deferred = promise.promise({}); deferred.resolveWith = doneList.fireWith; deferred.resolve = doneList.fire; // All done! return deferred; } });


  • Django Filtering A Blog Based on Tags
  • Android Random Number llegalArgumentException: n
  • Getting Geometry length
  • <? extends > Java syntax
  • Crash CGDataProviderCreateWithCopyOfData: vm_copy failed: status 1
  • Why is ftp_nlist returning an empty array for a non-empty directory?
  • Submit form without reloading or leaving current page (php- mysql)
  • iPhone - Strike out an NSString [duplicate]
  • Google Charts: Animation doesn't work for one chart, pointing annotations in the title area
  • Retrieving custom entities in CRM 4 C#
  • Cycle R,G,B vales as HUE?
  • View.layout() works until next UI update
  • Use Perl to Add GIF Image Other Than 8-bit to PDF
  • Application backgrounding in Xamarin.Mac
  • Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php
  • Anaconda + Apache + mod_wsgi + Ubuntu
  • Calling a flash ExternalInterface in swiffyobject
  • Unable to connect to AWS RDS through PDO
  • Issues with converting data into a matrix after running lapply()
  • How to process remote XML files with XSLT
  • Authorize Attribute Authentication with Postman in Web Api
  • Set initial vuetify v-select value
  • view details for exception in vs 2017
  • Fortran function variable length string return
  • Classes in Python
  • Controller or RestController
  • How to use array in autohotkey?
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Calculate time from document
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • jQuery scrollTop if URL has hash
  • Google App Engine backend servlet not responding
  • Computing the discrete fourier transform of audio data with FFTW
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007