Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

ajax - Await is not working inside a method javascript

I have an ajax call which return some data. Code is:

function ProductCodeLookUp() {
        return $.ajax({
            url: "/ProductInventory/GetProductCodeHirerchy",
            type: "get", //send it through get method
            async: false,
            data: {
                productCode: $('#ProductCodeText_Id').val()
            },
            success: function (response) {
                debugger;
                if (response != null && (response.tissueClassificationType != null || response.tissueClassificationType != null)) {
                    $('#productCodeTable_id').show();
                    $('#ProductCodeNotExistsWarning_id').hide();

                    //addOptionsToSelect(response, 'size_id', $('input[id=size_id]').val());
                    $('#ProductCodeTextClassification_Id').text(response.tissueClassificationType);
                    $('#ProductCodeTextTissueType_Id').text(response.tissueType);
                    $('#ProductCodeTextProduct_Id').text(response.tissueDefinition);
                    $('#ProductCodeTextSize_Id').text(response.tissueDefinitionSize);
                }
                else {
                    $('#ProductCodeNotExistsWarning_id').show();
                    $('#productCodeTable_id').hide();
                }
            },
            error: function (xhr) {
                //Do Something to handle error
            }
        });
    }

I Have to take some decision on the basis of the data returned by this Ajax call. The method where i am calling ProductCodeLookUp() method is:

async function checkRequiredFields() {
var isValid = false;
if (checkRequiredFieldsSubMethod() == true) {
            if (AddProductInBulk() == true) {
                if (QualityChecksValidation() == true) {
                    debugger;
                    var res = await ProductCodeLookUp();
                    if (res.tissueDefinitionSize != null)
                        isValid = true;
                    else
                        isValid = false;
                  }
                }
         }
        debugger;
        return isValid;
    }

Issue is that it does not await on the ProductCodeLookUp() method. I have to wait for the result of this method and on the basis of result make a decision that whether to return true or false. But i am unable to do this. What can be the possible issue.

question from:https://stackoverflow.com/questions/65950584/await-is-not-working-inside-a-method-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wrap the ajax call in a promise that resolves on success and rejects on error.

function ProductCodeLookUp() {
  return new Promise((resolve, reject) => $.ajax({
    // ...
    success: function (response) {
      // ...
      resolve();
    },
    error: function (response, status, err) {
       // ...
       reject(err);
    }
  }));
}   

Also, set async to true


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...