0

I'm working on an inherited project, I have a scenario when a function like this is being called from say... 15 different places simultaneously.

var myCall = function () {
    var dfd = $.Deferred();
    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        url: 'xxxxxxx',
        data: { },
        success: function(response) {
            dfd.resolve();

        }
    });
    return dfd.promise();
};

I want the first call to this function to actually go out and get the data, I want the following calls to just get a promise that gets resolved when the first call finishes. I may be missing something fundamental so I thought I'd ask. Do I need to create an array of promises adding a new promise on every call then resolve all of them when the data has been retrieved?

  • Avoid the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572)! – Bergi Apr 02 '16 at 16:55
  • No, you don't need to create an array of promises, you can hand out the very same promise to all your callers. Maybe also have a look at [this question](http://stackoverflow.com/q/18744830/1048572). – Bergi Apr 02 '16 at 17:36
  • Exactly what I was looking for, thanks very much Bergi – Dan Budimir Apr 04 '16 at 12:27

0 Answers0