美文网首页我爱编程
jquery封装Promise

jquery封装Promise

作者: leesession | 来源:发表于2018-04-11 20:20 被阅读0次

1.自定义一个方法,如:Ajax,此处我传了2个参数

var Ajax = function (options, url) {
    return new Promise(function (resolve, reject) {
        $.ajax({
            type: "post",
            data: options,
            url: url,
            success: function (res) {
                resolve(res);
            }
        })
    })
};

2.在使用的时候

Ajax(options,url).then(function(value){
  // value 是后台返回的数据,then 表示接口返回成功
console.log(value)
//return 是返回 连续的第二个 接口
return Ajax(options,url);//接口2
}).then(function(value){
  //此处的value为 上面接口2返回的数据
})

相关文章

网友评论

    本文标题:jquery封装Promise

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