Javascript 封装在函数中的异步AJAX请求赢得';Don’我不能答应你

Javascript 封装在函数中的异步AJAX请求赢得';Don’我不能答应你,javascript,jquery,ajax,asynchronous,Javascript,Jquery,Ajax,Asynchronous,我正在执行异步AJAX请求,这些请求被包装到函数中。其中,$.ajax是延迟对象,我可以正确地使用.promise(检查:初始加载),那么我将无法对“现在真正加载”执行相同的操作,这将在ajax完成加载之前执行 function WSCall(method, data, callback, type, async, bg) { // .. code .. var promise = $.ajax({ 'url': useSampleData ? useSample

我正在执行异步AJAX请求,这些请求被
包装到函数中。其中,$.ajax是延迟对象,我可以正确地使用.promise(检查:初始加载),那么我将无法对“现在真正加载”执行相同的操作,这将在ajax完成加载之前执行

function WSCall(method, data, callback, type, async, bg) {
    // .. code ..
    var promise = $.ajax({
        'url': useSampleData ? useSampleData || null,
        //'async': false,
        'type': 'POST',
        'dataType': (type == null) ? 'json' : type,
        'data': data,
        'beforeSend': bg ? null : LoadingBegin,
        'complete': bg ? null : LoadingEnd,
        'success': callback,
        'error' : bg ? null : function(jqXHR, textStatus, errorThrown) { networkError = 1; }
    });

    promise.done(function(){ console.log('Initially loaded') });
}

function aSyncEvent() { 
    WSCall(
        'status',
        {},
        function (data) {
            if (data.error) { 
                console.log('Error occured'); return ShowDialogAlert(data.error); }
            if (data.statusResult) {
                var parts = data.statusResult.split('-');
                if (parts[1] === '0') { 
                    sId = parts[0]; 
                    console.log('Wow its loaded!'); 
                    return true; 
                }               
            }
        }
    )   
}

$.when( aSyncEvent() ).then( function () { console.log('now really loaded')});
初始加载和Wow加载将在ajax按正确顺序执行后正确显示,但“现在真正加载”将在ajax完成执行之前显示

关于这件事我请求帮助

谢谢
迈克

你试过退回你的延期付款吗

function WSCall(method, data, callback, type, async, bg) {
    // .. code ..
    return promise.done(function(){ console.log('Initially loaded') });
}


From:“如果将一个参数传递给
jQuery。当
不是延迟时,它将被视为已解决的延迟,任何附加的doneCallbacks都将立即执行。”感谢nnnnnn提供的信息,我仍然想知道promise()如何检查是否已解决了derred。这不是简单的如果(真的)?
function aSyncEvent() { 
    return WSCall(
        // .. code ..
    );
}