Dev/Javascript, CSS

jquery deferred promise 예제

Fehoon- 2018. 7. 24. 18:12

deferred , promise



function validateUserDetails() {
    var deferred = $.Deferred();
    var bool = false;

    $.ajax({
            url: 'response.php?type=validateUserDetails',
            type: 'POST',
            dataType: 'json',
            data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(), "city": $("#checkout_city").val()},
            success: function(data) {
                console.log(data);  // this is currently returning FALSE
                                    // Which is totally correct!
                if (data == true) {
                    bool = true;
                }
            }
            complete: function () {
                deferred.resolve(trueOrFalse(bool));
            }
    });

    return deferred.promise();
}

function test() {
    var promise = validateUserDetails();
    promise.done(function(result) {
        console.log("Bool: " + result);
    });
}


참고 


https://stackoverflow.com/questions/23078650/ajax-return-true-false-i-have-implemented-a-callback

https://www.zerocho.com/category/jQuery/post/57c90814addc111500d85a19

반응형