// 获取奶茶的方法
function getTea(fn){
setTimeout() => {
fn("奶茶")
},500)
}
function getHotpot(fn){
setTimeout(()=>{
fn("火锅")
},800)
}
// 调用获取奶茶的方法
getTea(function(data){
console.log(data)
})
getHotpot(function(data){
console.log(data)
getTea(function(data){ // 喝奶茶
console.log(data)
getTea(function(data){ // 喝奶茶
console.log(data)
})
})
})
// promise 对选哪个
// resolve 可以将异步的数据传递出来
let p = new Promise(function(resolve){
resolve("hello wolrd")
})
// 通过 then 拿到异步数据
p.then(fucntion(data){
console.log(data);
})
// 获取奶茶的方法
function getTea(){
return new Promise(function(resolve){
setTimeout(()=>{
resolve("奶茶")
},1000)
})
}
getTea().then(function(data){
console.log(data)
})
function getHotpot(){
return new Promise(function(relsolve){
setTimeout(()=>{
resolve("火锅")
},2000)
})
}
// 先吃火锅、再喝奶茶
getHotpot().then(fucntion(data){
console.log(data);
return getTea();
}).then(fucntion(data){
console.log(data)
})
// async 函数
async function getData(){
// 直接获取 resolve 传递出来的异步数据
let hotPot = await getHotpot();
console.log(hotPot);
let tea = await getTea();
console.log(tea)
}
getData();
JS基础(六)--回调地狱与Promise对象
于 2022-04-14 09:51:27 首次发布
关键词由CSDN通过智能技术生成