回调函数实现先拿火锅再拿奶茶功能
function getTea(fn){
setTimeout(()=>{
fn('奶茶')
},500)
}
function getHotpot(fn){
setTimeout(()=>{
fn('火锅')
},800)
}
getTea(function(data){
console.log(data)
})
getHotpot(function(data){
console.log(data)
})
getHotpot(function(data){
console.log(data)
getTea(function(data){
console.log(data)
})
})
Promise 对象举例
let p = new Promise(function(resolve){
resolve('hello word')
})
p.then(function(data){
console.log(data)
})
Promise 实现先拿火锅再拿奶茶功能
function getTea() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve('奶茶')
}, 500)
})
}
getTea().then(function(data){
console.log(data)
})
function getHotpot() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve('火锅')
}, 800)
})
}
getHotpot().then(function(data){
console.log(data)
})
getHotpot().then(function(data){
console.log(data)
return getTea()
}).then(function(data){
console.log(data)
})
async函数实现先拿火锅再拿奶茶功能
function getTea() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve('奶茶')
}, 500)
})
}
getTea().then(function(data){
console.log(data)
})
function getHotpot() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve('火锅')
}, 800)
})
}
getHotpot().then(function(data){
console.log(data)
})
async function getData(){
let hotPot = await getHotpot()
console.log(hotPot)
let tea = await getTea()
console.log(tea)
}
getData()