0

My Requirement is return data using "SetUnit()" function but this function is contain async AJAX services so i am using Deferred suggested in this (http://jsfiddle.net/b69ubLa0/21/) link. but this is not working on my requirement.

Code......

Function

function SetUnit(query) {
            var $q = new $.Deferred();
            var oData = [];
            var filter = JSON.stringify({ 'param': query });
            $.ajax({
                type: "POST",
                url: '../WebService.asmx/ExecuteReader',
                contentType: "application/json; charset=utf-8",
                data: filter,
                dataType: "json",
            }).fail(function (jqXHR, textStatus, errorThrown) {
                $q.reject(jqXHR, textStatus, errorThrown);
            }).done(function (data, textStatus, jqXHR) {
                return $q.resolve(data);
            });
            return $q.promise();
        }

Call Function 1

var oUNIT_NAME = SetUnit(query).done(function (data) { return data; });

Call Function 2

 var oUNIT_NAME = SetUnit(query);

call function using 2 style.

And This "oUNIT_NAME" var is using for binding many drop downs.

Services Return JSON OBJECT

[{id:1,name:a},{id:1,name:a},{id:1,name:a},{id:1,name:a}]

Note : this is working if i am add (async: false) in AJAX setting but this not a good practice and this block my UI.

Abhishek Tomar
  • 827
  • 1
  • 10
  • 20
  • You've successfully stored a promise in the variable `oUNIT_NAME`, but it seems that you want `oUNIT_NAME` to hold a resolved value, not a promise object, correct? The solution is to set `oUNIT_NAME` inside the `done` callback. This may be a duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/710446) – apsillers Apr 13 '17 at 17:27
  • Check this for more detail i am update my code but still not working... URL : https://github.com/mistic100/jQuery-QueryBuilder/issues/466. please help. – Abhishek Tomar Apr 13 '17 at 18:16
  • Does your "Call Function 1" example run? That is: if you do `SetUnit(query).done(function (data) { alert(data); });` does the value get alerted? If so, you just need to do all your work inside the callback, or inside another function that is called from the callback. – apsillers Apr 13 '17 at 18:20
  • Thanks - issue is resolved thanks for support . please suggest best website for callback and "Deferred" tuts. – Abhishek Tomar Apr 13 '17 at 18:32
  • as `$.ajax` returns a `Promise` there is no need for `$.Deferred` – Jaromanda X Apr 14 '17 at 00:34
  • **`this is working if i am add (async: false) in AJAX setting`** I highly doubt that, seeing as either way you are returning a promise, so regardless of async flag, you will always get a promise which you seem to be not expecting – Jaromanda X Apr 14 '17 at 00:36

1 Answers1

2

I hope this helps. I'm not a pro about deferred objects but i believe this does what you need.

updated

Working Fiddle

var oUNIT_NAME = {};
function SetUnit(query) {
    var $q = new $.Deferred()
    $q.promise(oUNIT_NAME);
    oUNIT_NAME.done(function(data) {
        //Bind your dropdowns
        alert(JSON.stringify(data));
    }).fail(function(jqXHR, textStatus, errorThrown) {
        //Alert user
        alert(errorThrown);
    });
    var oData = [];
    var filter = {
        json: JSON.stringify({
            data: query
        })
    };
    return $.ajax({
        cache: false,
        type: "POST",
        url: '/echo/json/',
        data: filter,
        dataType: "json"
    }).fail(function(jqXHR, textStatus, errorThrown) {
        $q.reject(jqXHR, textStatus, errorThrown);
    }).done(function(data, textStatus, jqXHR) {
        $q.resolve(data);
    });
}

SetUnit({
    param: 123
})
$("button").click(function() {
    SetUnit({
        param: 456
    })
});
Ergec
  • 11,608
  • 7
  • 52
  • 62
  • Hay thanks – This is really helpful for me, I’ll appreciate you for help thanks very much… – Abhishek Tomar Apr 14 '17 at 10:58
  • Sorry - I am new on Stackoverflow. how to accept answers. and this is very help full for me.... – Abhishek Tomar Apr 17 '17 at 15:12
  • // (THIS CODE IS RIGHT FOR CREATE AJAX ASYNC based PLUGIN PLEAE SUGGECT) $.fn.Set_DDL_Data = function (query) { var filter = JSON.stringify({ 'param': query }); return $.ajax({ type: "POST", url: '../WebService.asmx/ExecuteReader', contentType: "application/json; charset=utf-8", data: filter, dataType: "json", }).fail(function (jqXHR, textStatus, errorThrown) { $q.reject(jqXHR, textStatus, errorThrown); }).done(function (data, textStatus, jqXHR) { $q.resolve(data.d); }); $q.promise(); }; – Abhishek Tomar Apr 17 '17 at 18:27