Taja_100 Taja_100 - 1 year ago 60
jQuery Question

Deferred.done not execute chainable process

i read the link https://api.jquery.com/deferred.done/

then i edit my code : as follows

type: 'Get',
dataType: 'json',
data: { id: id },
url: '@Url.Action("GetInsuranceClaim","Insurance")',
success: function (da) {
if (da.Message == "Success") {
var d = $.Deferred();
d.done($('#Cust_id').val(da.Result[0].customer_id), GetPolicy(), $('#policy_no').val(da.Result[0].policy_detail_id), console.log(da.Result[0].policy_detail_id));

d.done($('#Cust_id').val(da.Result[0].customer_id), GetPolicy(),
$('#policy_no option').val(da.Result[0].policy_detail_id),

GetPolicy() is ajax call my problem is ajax call is executing last,

i required execution order

  1. $('#Cust_id').val(da.Result[0].customer_id)

    1. GetPolicy()

    2. $('#policy_no').val(da.Result[0].policy_detail_id)

Answer Source

Try the code snippet below - note the use of return in AJAX calls, it is important to use it so you can chain deferreds.

Note: AJAX call is also Deferred!

$.GetInsuranceClaim = function(){
     return $.ajax({
                type: 'Get',
                dataType: 'json',
                data: { id: id },
                url: '@Url.Action("GetInsuranceClaim","Insurance")',

$.GetPolicy = function(){
     //AJAX call - basically the same as the above (don't forget return)

          ///logic you want to do with this response