Promise

2018-07-07  本文已影响0人  Cicada丶
promise承诺在指定任务序列完成后执行回调,这个任务可以是同步任务也可以是异步任务
var d1 = $.Deferred();
$.ajax({
    url: 'http://localhost:8080/target',
    type: 'get',
    dataType:'json',
    success: function (data) {
        d1.resolve(data)
    },
    fail:function (event) {

    }
});

var d2 = $.Deferred();
$.ajax({
    url: 'http://localhost:8080/target',
    type: 'get',
    dataType:'json',
    success: function (data) {
        d2.resolve(data);
    },
    fail:function (event) {

    }
});
$.when(
    d1.promise(),
    d2.promise()
).then(function(p1,p2) {
    alert("p1:"+p1.id);
    alert("p2:"+p2.name);
});

});
var promise1 = $.ajax({
    url: 'http://localhost:8080/target',
    type: 'get',
    dataType:'json',
    success: function (data) {
    },
    fail:function (event) {

    }
}).promise();

var promise2 = $.ajax({
    url: 'http://localhost:8080/target',
    type: 'get',
    dataType:'json',
    success: function (data) {
    },
    fail:function (event) {

    }
}).promise();
$.when(
promise1,
promise2
).then(function() {
});
上一篇下一篇

猜你喜欢

热点阅读