5-1创建Promise对象
promise函数中传递两个参数
resolve —>处理成功 then去触发resolve
reject —>处理失败 catch去触发reject
//创建Promise对象
promise函数中传递两个参数
resolve -->处理成功
reject -->处理失败
const p=new Promise(function(resolve,reject){
//成功
// let data="成功了"
// resolve(data);
//失败
let data="失败了"
reject(data)
})
//调用Promise then方法
p.then(fuction(value){
console.log(value)
},functiom(reason){
console.log(reson)
})
5-2封装ajax
function http(url){
return new Promise((resolve,reject)=>{
$.ajax({
url,
success:res=>{
resolve(res)
},
error:err=>{
reject(err)
}
})
})
}
var url=`http://47.108.197.28:3000/top/playlist`;
http(url).then(function(res){
console.log(res);
})
5-3封装http
var baseUrl='http://47.108.197.28:3000'
function http(url){
return new Promise((resolve,reject)=>{
wx-wx.request({
url:baseUrl+url,
success: res => {
resolve(res)
},
fail: (err) => {
reject(err)
},
})
})
}
function getMvHttp(){
return http("/mv/first")
}
function getMvDetailHttp(id){
return http(`mv/url?id=${id}`)
}
module.exports={
getMvHttp,
getMvDetailHttp
}