Promise

作者: Cicada丶 | 来源:发表于2018-07-07 01:23 被阅读0次
    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() {
    });
    
    • bluebird.js可以解决ie浏览器不支持promise语法的问题

    相关文章

      网友评论

          本文标题:Promise

          本文链接:https://www.haomeiwen.com/subject/ueqneftx.html