首页 > jQuery无法在标准promise中使用,求解

jQuery无法在标准promise中使用,求解

使用最新的jQuery,不是说“1.5版本之后,$.ajax()的返回对象实现了CommonJS的Promises/A接口”了吗?


function getJSON(){
    var promise = new Promise(function(resolve,reject){
        jQuery.post('http://127.0.0.1').then(function(data){
            resolve(data)
        })
    })
    return promise
}

jQuery.post('http://127.0.0.1').then(function(){
    return Promise.all([sonthing])
}).then(function(result){
    console.log('result不正常')
})

getJSON().then(function(){
    return Promise.all([sonthing])
}).then(function(result){
    console.log('result正常')
})

希望大神解答,谢谢


可能跟你使用的jquery版本有一定关系。

1.5之前返回的是xhr对象,省略。

1.5之后,返回的是deferred对象:

var promise=$.ajax({
    //
});
promise.done(function(){});
promise.fail(function(){});
promise.always(function(){});

1.8之后返回的才是promise对象,注意1.8之前的deferred对象是有then方法的,但不是promise对象。

【热门文章】
【热门文章】